当前位置: 首页 > news >正文

福建设厅官方网站wordpress调用文章内容

福建设厅官方网站,wordpress调用文章内容,天威虎建设集团官方网站,wordpress 代码大全摘要 博文主要介绍二叉树的前/中/后/层遍历(递归与栈)方法 一、前/中/后/层遍历问题 144. 二叉树的前序遍历 145. 二叉树的后序遍历 94. 二叉树的中序遍历 102. 二叉树的层序遍历 二、二叉树遍历递归解析 // 前序遍历递归LC144_二叉树的前序遍历 class Solution {publi…摘要 博文主要介绍二叉树的前/中/后/层遍历(递归与栈)方法 一、前/中/后/层遍历问题 144. 二叉树的前序遍历 145. 二叉树的后序遍历 94. 二叉树的中序遍历 102. 二叉树的层序遍历 二、二叉树遍历递归解析 // 前序遍历·递归·LC144_二叉树的前序遍历 class Solution {public ListInteger preorderTraversal(TreeNode root) {ListInteger result new ArrayListInteger();preorder(root, result);return result;}public void preorder(TreeNode root, ListInteger result) {if (root null) {return;}result.add(root.val);preorder(root.left, result);preorder(root.right, result);} }// 中序遍历·递归·LC94_二叉树的中序遍历 class Solution {public ListInteger inorderTraversal(TreeNode root) {ListInteger res new ArrayList();inorder(root, res);return res;}void inorder(TreeNode root, ListInteger list) {if (root null) {return;}inorder(root.left, list);list.add(root.val); // 注意这一句inorder(root.right, list);} }// 后序遍历·递归·LC145_二叉树的后序遍历 class Solution {public ListInteger postorderTraversal(TreeNode root) {ListInteger res new ArrayList();postorder(root, res);return res;}void postorder(TreeNode root, ListInteger list) {if (root null) {return;}postorder(root.left, list);postorder(root.right, list);list.add(root.val); // 注意这一句} }三、二叉树遍历栈解析 // 前序遍历顺序中-左-右入栈顺序中-右-左 class Solution {public ListInteger preorderTraversal(TreeNode root) {ListInteger result new ArrayList();if (root null){return result;}StackTreeNode stack new Stack();stack.push(root);while (!stack.isEmpty()){TreeNode node stack.pop();result.add(node.val);if (node.right ! null){stack.push(node.right);}if (node.left ! null){stack.push(node.left);}}return result;} }// 中序遍历顺序: 左-中-右 入栈顺序 左-右 class Solution {public ListInteger inorderTraversal(TreeNode root) {ListInteger result new ArrayList();if (root null){return result;}StackTreeNode stack new Stack();TreeNode cur root;while (cur ! null || !stack.isEmpty()){if (cur ! null){stack.push(cur);cur cur.left;}else{cur stack.pop();result.add(cur.val);cur cur.right;}}return result;} }// 后序遍历顺序 左-右-中 入栈顺序中-左-右 出栈顺序中-右-左 最后翻转结果 class Solution {public ListInteger postorderTraversal(TreeNode root) {ListInteger result new ArrayList();if (root null){return result;}StackTreeNode stack new Stack();stack.push(root);while (!stack.isEmpty()){TreeNode node stack.pop();result.add(node.val);if (node.left ! null){stack.push(node.left);}if (node.right ! null){stack.push(node.right);}}Collections.reverse(result);return result;} }四、二叉树层序遍历解析 // 102.二叉树的层序遍历 class Solution {public ListListInteger resList new ArrayListListInteger();public ListListInteger levelOrder(TreeNode root) {//checkFun01(root,0);checkFun02(root);return resList;}public void checkFun02(TreeNode node) {if (node null) return;QueueTreeNode que new LinkedListTreeNode();que.offer(node);while (!que.isEmpty()) {ListInteger itemList new ArrayListInteger();int len que.size();while (len 0) {TreeNode tmpNode que.poll();itemList.add(tmpNode.val);if (tmpNode.left ! null) que.offer(tmpNode.left);if (tmpNode.right ! null) que.offer(tmpNode.right);len--;}resList.add(itemList);}} }博文参考 《leetcode》
http://www.zqtcl.cn/news/125580/

相关文章:

  • 万网网站空间多少钱一年做哪些网站流量最大
  • seo网站优化服务网站开发电脑
  • 宿迁怎样建设网站wordpress 分类目录 标签
  • 惠州双语网站建设费用seo搜索工具栏
  • 做ppt会去什么网站找图网页制作与网站建设试题
  • 如何用ai给网站做logo宝安网站制作公司
  • sem是什么职业岗位单页式网站 seo
  • 做网站用什么版本系统国外有哪些设计网站推荐
  • dz论坛怎么做视频网站吗哪些公司是wordpress
  • 在微信怎样搞做微视频网站商城小程序模板源码完整版
  • h5跟传统网站有啥区别读取wordpress最新文章
  • 网站推广120最超值的网站建设
  • 移动网站制作公司如何开公司做网站
  • 网站建设 青海试题wordpress的特点
  • 源码如何做网站宽甸县建设局网站
  • 用dw做的网页怎么连到网站上企业网站备案资料填写单
  • 中文 网站模板企业怎么建设网站
  • 建设户外腰包网站哪个网站注册域名好
  • 六安网站建设价格小学生编程网课前十名
  • 绵阳网站建设信赖辉煌wordpress多账号权限
  • 网站外链快速建设网站维护要学多久
  • 做网站都是用ps吗郑州网站设计培训
  • wordpress 多站点教程厦门做网站维护的公司
  • 婚纱网站建设需求分析wordpress js图片
  • seo网站怎么优化有哪些企业网站平台
  • 响应式中文网站欣赏wordpress 带分页的主题
  • 什么样的网站可以做站内站房地产的设计网站建设
  • 成都住房和城乡建设局 网站首页深圳西乡建网站
  • 商城类的网站一般怎么做开发app软件的步骤
  • 招聘网站做销售怎么样做网站后台学什么专业