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

精湛的网站建设排行榜企业的品牌宣传策划

精湛的网站建设排行榜,企业的品牌宣传策划,网页设计图模板,自己做的网站怎么赚钱吗摘要 博文主要介绍二叉树的前/中/后/层遍历(递归与栈)方法 一、前/中/后/层遍历问题 144. 二叉树的前序遍历 145. 二叉树的后序遍历 94. 二叉树的中序遍历 102. 二叉树的层序遍历 103. 二叉树的锯齿形层序遍历 二、二叉树遍历递归解析 // 前序遍历递归LC144_二叉树的前…

摘要

博文主要介绍二叉树的前/中/后/层遍历(递归与栈)方法

一、前/中/后/层遍历问题

144. 二叉树的前序遍历

145. 二叉树的后序遍历

94. 二叉树的中序遍历

102. 二叉树的层序遍历

103. 二叉树的锯齿形层序遍历

二、二叉树遍历递归解析

// 前序遍历·递归·LC144_二叉树的前序遍历
class Solution {public List<Integer> preorderTraversal(TreeNode root) {List<Integer> result = new ArrayList<Integer>();preorder(root, result);return result;}public void preorder(TreeNode root, List<Integer> result) {if (root == null) {return;}result.add(root.val);preorder(root.left, result);preorder(root.right, result);}
}// 中序遍历·递归·LC94_二叉树的中序遍历
class Solution {public List<Integer> inorderTraversal(TreeNode root) {List<Integer> res = new ArrayList<>();inorder(root, res);return res;}void inorder(TreeNode root, List<Integer> list) {if (root == null) {return;}inorder(root.left, list);list.add(root.val);             // 注意这一句inorder(root.right, list);}
}// 后序遍历·递归·LC145_二叉树的后序遍历
class Solution {public List<Integer> postorderTraversal(TreeNode root) {List<Integer> res = new ArrayList<>();postorder(root, res);return res;}void postorder(TreeNode root, List<Integer> list) {if (root == null) {return;}postorder(root.left, list);postorder(root.right, list);list.add(root.val);             // 注意这一句}
}

三、二叉树遍历栈解析

 

// 前序遍历顺序:中-左-右,入栈顺序:中-右-左
class Solution {public List<Integer> preorderTraversal(TreeNode root) {List<Integer> result = new ArrayList<>();if (root == null){return result;}Stack<TreeNode> 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 List<Integer> inorderTraversal(TreeNode root) {List<Integer> result = new ArrayList<>();if (root == null){return result;}Stack<TreeNode> 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 List<Integer> postorderTraversal(TreeNode root) {List<Integer> result = new ArrayList<>();if (root == null){return result;}Stack<TreeNode> 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 List<List<Integer>> resList = new ArrayList<List<Integer>>();public List<List<Integer>> levelOrder(TreeNode root) {//checkFun01(root,0);checkFun02(root);return resList;}public void checkFun02(TreeNode node) {if (node == null) return;Queue<TreeNode> que = new LinkedList<TreeNode>();que.offer(node);while (!que.isEmpty()) {List<Integer> itemList = new ArrayList<Integer>();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.yayakq.cn/news/918429/

相关文章:

  • 汽车4S店网站建设做招聘网站怎么样
  • 建设向58同城的网站网站设计方案范本
  • 嘉兴企业网站推广方法hexo导入wordpress
  • ip做网站域名朔州市2018年建设工程网站
  • 钓鱼网站图片html网页制作模板代码简单
  • 南通市建设工程安全监督站网站国外可以用什么网站做问卷
  • 物流案例 网站网站的推广方式包括
  • 建立网站专业公司wordpress网站如何搬家
  • seo整站优化平面设计配色
  • 漳浦县城乡规划建设局官方网站荥阳网站建设公司哪家好
  • 三网合一网站方案网站哪家做的好
  • 宣城老品牌网站建设专做畜牧招聘网站的
  • 闸北区网站设计与制作网站建设制作收费
  • 导购网站开发要多少钱西安十大平面广告设计公司
  • 西安惠安小学网站建设深圳推广公司网站建设书模板
  • 领卷网站怎么做的如何分析网站
  • 自适应网站建设方案简述电子商务网站的建设步骤
  • 泰州网站建设策划网站开发公司开发过程
  • 手机如何搭建网站怀化网站优化公司哪家好
  • 青羊区区建设局网站wordpress 一键转发
  • 北京网站手机站建设公司吗中国空间站天宫课堂
  • 网站内页301重定向怎么做深圳在哪些网站找什么好处
  • 什么是网站托管iis做本地视频网站
  • 重庆网站建设熊掌号html中文美食网站
  • 旅游网站建设流程cms网站建设的实训总结
  • 手机网站开发公司荆州网站建设厂家
  • 佛山网站建设冯哥无货源电商选品软件
  • 新乡哪里做网站上海十大工业设计公司
  • 米拓 wordpress企业seo可以达到怎样的效果
  • 清空回收站 wordpress中国国际新闻