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

二叉树问题——前/中/后/层遍历(递归与栈)

摘要

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

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

144. 二叉树的前序遍历

145. 二叉树的后序遍历

94. 二叉树的中序遍历

102. 二叉树的层序遍历

二、二叉树遍历递归解析

// 前序遍历·递归·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.lryc.cn/news/212324.html

相关文章:

  • Nor Flash和Nand Flash的区别——笔记
  • 7+共病思路。WGCNA+多机器学习+实验简单验证,易操作
  • 开发者看亚马逊云科技1024【文末有福利~】
  • 操作系统(Linux)外壳程序shell 、用户、权限
  • C文件操作
  • drawio特性
  • LLM-Embedder
  • xsync 集群远程同步脚本
  • 30秒get视频号视频如何下载,保存视频号视频到本地方法!
  • 优化改进YOLOv5算法:加入SPD-Conv模块,让小目标无处遁形——(超详细)
  • 【数据结构】搜索树 与 Java集合框架中的Set,Map
  • 掌握组件缓存:解开Vue.js中<keep-alive>的奥秘
  • Ajax学习笔记第5天
  • 20.1 OpenSSL 字符BASE64压缩算法
  • Panda3d 教程
  • 除自身以外数组的乘积
  • 干洗店小程序上门洗鞋店管理软件功能介绍;
  • 【C语言初学者周冲刺计划】1.1用筛选法求100之内的素数
  • 1.Vue—简介、实例与容器、MVVM模型
  • 【Java笔试强训】Day7(WY22 Fibonacci数列、CM46 合法括号序列判断)
  • Linux进程的概念
  • XML教学视频(黑马程序员精讲 XML 知识!)笔记
  • 自定义组件实现v-model
  • 【自动驾驶】Free space与Ray casting
  • RHCE---正则表达式
  • 3D RPG Course | Core 学习日记一:初识URP
  • Spring Cloud 之RabbitMQ的学习【详细】
  • 第五章 I/O管理 六、I/O核心子系统
  • winfrom窗体比例缩放
  • 宇信科技:强势行业加速融入AIGC,同时做深做细