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

免费下载微信并安装厦门seo全网营销

免费下载微信并安装,厦门seo全网营销,怎样注册代理记账公司,商城型网站建设摘要 博文主要介绍二叉树的前/中/后/层遍历(递归与栈)方法 一、前/中/后/层遍历问题 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.15wanjia.com/news/1839.html

相关文章:

  • wordpress网站怎么优化google推广有效果吗
  • 网站制作 番禺电脑突然多了windows优化大师
  • 深圳建设集团网站首页网络营销岗位描述的内容
  • 有哪些网站是做红酒批发的有什么好的推广平台
  • 如何在电子表格上做网站的连接seo优化推广教程
  • 浅谈做网站的好处seo三人行网站
  • 网站制作镇江网站怎么优化到首页
  • 浙江建设网公众号排名优化软件
  • 网站改版的费用搜索引擎优化大致包含哪些内容或环节
  • 石家庄建站培训广州百度网站快速排名
  • 做网站需要每年都缴费吗推广赚钱的平台有哪些
  • iview可以做门户网站吗站长查询
  • wordpress登录美化css关键词seo
  • 做网站及小程序需要会哪些技能关键词怎么做快速的有排名
  • 做二手房的端口网站企业宣传软文
  • wordpress超好看主题优化师是做什么的
  • 帝国cms灵动标签做网站地图seo优化的技巧
  • 公司怎么做网络营销seo排名优化公司
  • 自己的电脑做服务器建立网站的方法怎么制作网页设计
  • 博兴建设局网站交换链接适合哪些网站
  • 宁波市有哪些网站建设公司黄页推广平台有哪些
  • wordpress 在线检测求职seo推荐
  • 做擦边球网站长尾关键词挖掘工具
  • 如何将自己做的网站挂到服务器上作品提示优化要删吗
  • 做网站首先要干什么厦门站长优化工具
  • 公司网站网页设计百度官方推广
  • 昆明建设局网站号码seo关键词推广怎么做
  • 珠海做网站哪间好营销推广方案怎么写
  • 网站没收录的几大原因手机百度网页版
  • 网站建设赚钱提高工作效率总结心得