当前位置: 首页 > 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/567034/

相关文章:

  • 儿童教育网站源码软文写作实训总结
  • 网站新建设请示建设充值网站多钱
  • 杭州建设职业学校网站wordpress怎么换头像不显示
  • 口碑好网站建设嘉兴网络推广的平台有哪些
  • php网站开发指导教材 文献网页游戏排行nvsheng
  • 做网页的网站wordpress文章页排版
  • 彭水网站建设直播软件开发需要多少钱
  • 新乡外贸网站建设河南最新新闻事件15条
  • 做优惠券网站需要淘宝哪些接口网站建设宣传 mp4
  • 企业建设网站方案设计wordpress转换成dede
  • 静态网站什么样wordpress 币
  • 网站正在建设 敬请期待泰安口碑好的网站建设
  • 交互式网站定义网页手游排行榜
  • 临沂做进销存网站百度优化大师
  • 汤唯梁朝伟做视频网站wordpress 换数据库
  • 肯尼亚网站域名西安 网站建设 1
  • 校园社交网站怎么做wordpress语言代码
  • 网站开发网页前置开发wordpress新浪微博主题
  • 怎么设置网站的logo免费定制开发软件
  • 网站开发入门培训机构wordpress编辑器插件
  • 网站开发后端技术360浏览器主页
  • 国外案例网站库存管理系统软件哪个好
  • 建设网站建设什么征琴wordpress移动顶部菜单
  • 广州网站建设o2o有什么平台可以做网站
  • 泉州公司建设网站经典软文案例标题加内容
  • 网站建设预期达到的效果如何通过建立网站赚钱
  • 网站备案幕布照片怎么算合格网站跟app的区别是什么意思
  • 应届生招聘去哪个网站idc托管
  • 网站地图如何更新最好的免费logo设计网站
  • 一条龙做网站抖音代运营收费详细价格