二叉树的遍历方式

news/2024/10/18 10:18:59/

文章目录

  • 层序遍历——队列实现
    • 分析
    • Java完整代码
  • 先序遍历——中左右
    • 分析
    • 递归实现
    • 非递归实现——栈实现
  • 中序遍历——左中右
    • 递归实现
    • 非递归实现——栈实现
  • 后续遍历——左右中
    • 递归实现
    • 非递归实现——栈加标志指针实现
  • 总结

层序遍历——队列实现

给你二叉树的根节点 root ,返回其节点值的 层序遍历 。 (即逐层地,从左到右访问所有节点)

分析

借助队列存储的方式实现。队列这个数据结构是先入先出的。

具体步骤:
1、将根节点入队
2、出队首节点,将队首节点的左右非空孩子入队
3、重复2操作直到队列为空

注意:Java的队列由linkedList实现的,这个需要注意一下。

Java完整代码

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/
class Solution {public List<List<Integer>> levelOrder(TreeNode root) {List<List<Integer>> res = new ArrayList<>();  if (root == null) {return res;}LinkedList<TreeNode> queue = new LinkedList<>(); // Java的队列由linkedList实现的queue.add(root);while (!queue.isEmpty()) {List<Integer> list = new ArrayList<>();int current_queue_size = queue.size();for (int i = 0; i < current_queue_size; i++) {TreeNode top = queue.getFirst();list.add(top.val);if (top.left != null) {queue.add(top.left);}if (top.right != null) {queue.add(top.right);}queue.removeFirst();}res.add(list);}return  res;}}

先序遍历——中左右

给你二叉树的根节点 root ,返回它节点值的 前序 遍历。

分析

前序遍历是先访问根节点再左孩子然后右孩子。

递归实现

class Solution {public List<Integer> preorderTraversal(TreeNode root) {List<Integer> res = new ArrayList<Integer>();preorder(root, res);return res;}public void preorder(TreeNode root, List<Integer> res) {if (root == null) {return;}res.add(root.val);preorder(root.left, res);preorder(root.right, res);}
}

非递归实现——栈实现

借助栈数据结构

 * Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/
class Solution {public List<Integer> preorderTraversal(TreeNode root) {List<Integer> result = new ArrayList<>();if (root == null) {return result;}Stack<TreeNode> stack = new Stack<>();TreeNode p = root;TreeNode tmp;while (p != null || !stack.isEmpty()) {// 一路向左if (p != null) {result.add(p.val);stack.push(p);p = p.left;} else {tmp = stack.pop();p = tmp.right;}}return result;}
}

中序遍历——左中右

递归实现

class Solution {public List<Integer> inorderTraversal(TreeNode root) {List<Integer> res = new ArrayList<Integer>();inorder(root, res);return res;}public void inorder(TreeNode root, List<Integer> res) {if (root == null) {return;}inorder(root.left, res);res.add(root.val);inorder(root.right, res);}
}

非递归实现——栈实现

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/
class Solution {public List<Integer> inorderTraversal(TreeNode root) {List<Integer> result = new ArrayList<>();if (root == null) {return result;}Stack<TreeNode> stack = new Stack<>();TreeNode p = root;TreeNode tmp;while (p != null || !stack.isEmpty()) {// 一路向左if (p != null) {stack.push(p);p = p.left;} else {tmp = stack.pop();result.add(tmp.val);p = tmp.right;}}return result;}
}

后续遍历——左右中

递归实现

class Solution {public List<Integer> postorderTraversal(TreeNode root) {List<Integer> res = new ArrayList<Integer>();postorder(root, res);return res;}public void postorder(TreeNode root, List<Integer> res) {if (root == null) {return;}postorder(root.left, res);postorder(root.right, res);res.add(root.val);}
}

非递归实现——栈加标志指针实现

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/
class Solution {public List<Integer> postorderTraversal(TreeNode root) {List<Integer> result = new ArrayList<>();if (root == null) {return result;}Stack<TreeNode> stack = new Stack<>();TreeNode p = root;TreeNode tmp;TreeNode review = null;while (p != null || !stack.isEmpty()) {// 一路向左if (p != null) {stack.push(p);p = p.left;} else {tmp = stack.peek();if (tmp.right == null || review == tmp.right) {result.add(tmp.val);review = stack.pop();} else {p = tmp.right;}}}return result;}
}

总结

对于树的问题,更多能通过递归去简化问题,更好解决实际问题。

ps:计划每日更新一篇博客,今日2023-04-29,日更第十三天。
昨日更新:
反转字符串


http://www.ppmy.cn/news/58363.html

相关文章

( 字符串) 696. 计数二进制子串 ——【Leetcode每日一题】

❓696. 计数二进制子串 难度&#xff1a;简单 给定一个字符串 s&#xff0c;统计并返回具有相同数量 0 和 1 的非空&#xff08;连续&#xff09;子字符串的数量&#xff0c;并且这些子字符串中的所有 0 和所有 1 都是成组连续的。 重复出现&#xff08;不同位置&#xff09…

c#笔记-运算符

一元运算符 数字运算 正负 在数字前面&#xff0c;或数字类型的变量前面使用正负号&#xff0c;可以表示这个数值取自己&#xff0c;或是取相反数。 int i1 3; int i2 -3; int i3 i2;//-3 int i4 -i2;//3自增 一个数字类型在自己前面或后面连写两个或-&#xff0c;可以…

PySpark基础入门(3):RDD持久化

RDD的持久化 RDD 的数据是过程数据&#xff0c;因此需要持久化存储&#xff1b; RDD之间进行相互迭代的计算&#xff0c;新的RDD的生成代表着旧的RDD的消失&#xff1b;这样的特性可以最大化地利用资源&#xff0c;老旧地RDD可以及时地从内存中清理&#xff0c;从而给后续地计…

Android逆向实战(一)腾讯新闻去开屏广告

上次反编译一个工具类app失败&#xff0c;原因是使用了360加固&#xff0c;回编译后无法启动。一般来讲&#xff0c;大厂的app考虑到性能、兼容性、包体积等&#xff0c;通常不用加固。因此&#xff0c;本次我们选一个大一些的app-腾讯新闻。写在前面&#xff1a;本篇博客仅用来…

【LeetCode股票买卖系列:122. 买卖股票的最佳时机 II | 贪心 | 暴力递归=>记忆化搜索=>动态规划】

&#x1f680; 算法题 &#x1f680; &#x1f332; 算法刷题专栏 | 面试必备算法 | 面试高频算法 &#x1f340; &#x1f332; 越难的东西,越要努力坚持&#xff0c;因为它具有很高的价值&#xff0c;算法就是这样✨ &#x1f332; 作者简介&#xff1a;硕风和炜&#xff0c;…

PHP面试宝典之Mysql数据库基础篇

字符类型&#xff1a; tinyint(4)&#xff1a;占1个字节&#xff0c;4代表字段值长度&#xff0c;用0填充&#xff0c;搭配zero fill使用 有符号&#xff1a;取值范围 负128 &#xff5e; 正127&#xff1b; 无符号&#xff1a;取值范围 0 &#xff5e; 255&#xff1b; 默认无…

APK文件结构

文件结构 assets文件用来存放需要打包到Android 应用程序的静态资源文件&#xff0c;例如图片资源文件&#xff0c;JSON配置文件&#xff0c;渠道配置文件&#xff0c;二进制数据文件&#xff0c;HTML5离线资源文件等 与res/raw目录不同的数&#xff0c;assets目录支持任意深度…

【数据分析之道-NumPy(四)】numpy广播机制

文章目录 专栏导读1、广播机制2、一维数组和二维数组的广播3、二维数组和三维数组的广播4、标量和数组的广播5、形状不兼容的数组不能进行广播 专栏导读 ✍ 作者简介&#xff1a;i阿极&#xff0c;CSDN Python领域新星创作者&#xff0c;专注于分享python领域知识。 ✍ 本文录入…