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

力扣-94、144、145-前中后序遍历

二叉树遍历方法总结

 二叉树的遍历总体上分为深度优先遍历和广度优先遍历。常见的前中后序三种遍历方式就属于深度优先遍历,遍历过程中是顺着一条路径一直遍历到空节点然后向上回溯继续顺着遍历上一个节点的其他方向。层序遍历属于广度优先遍历,先遍历完同一层的节点,再接着遍历下一层节点。
 本文主要介绍二叉树三种深度优先遍历方式的实现方式:递归方式和非递归方式。
 递归方式实现如下。
 前序遍历-力扣144:

/*** 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 LinkedList<>();if (root == null) {return result;}preOrder(root,result);return result;}private void preOrder(TreeNode current,List<Integer> result) {if (current != null) {result.add(current.val);preOrder(current.left,result);preOrder(current.right,result);}}
}

 中序遍历-力扣94,就是把前序中的处理节点的顺序调整一下。

private void inOrder(TreeNode current,List<Integer> result) {if (current != null) {          inOrder(current.left,result);result.add(current.val);inOrder(current.right,result);}
}

 后序遍历-力扣145,将节点处理顺序调整到最后:

private void inOrder(TreeNode current,List<Integer> result) {if (current != null) {          inOrder(current.left,result);           inOrder(current.right,result);result.add(current.val);}
}

 非递归方式实现如下。
 前序遍历-力扣144,利用栈保存访问过的节点,每访问一个节点,就处理一个。

class Solution {public List<Integer> preorderTraversal(TreeNode root) {List<Integer> result = new LinkedList<>();if (root == null) {return result;}Deque<TreeNode> stack = new LinkedList<>();stack.push(root);while (!stack.isEmpty()) {TreeNode tempNode = stack.pop();result.add(tempNode.val);if (tempNode.right != null) {stack.push(tempNode.right);}if (tempNode.left != null) {stack.push(tempNode.left);}}return result;}
}

 中序遍历-力扣94

class Solution {public List<Integer> inorderTraversal(TreeNode root) {List<Integer> result = new LinkedList<>();if (root == null) {return result;}Deque<TreeNode> stack = new LinkedList<>();TreeNode current = root;while (!stack.isEmpty() || current != null) {if (current != null) {stack.push(current);current = current.left;} else {TreeNode tempNode = stack.pop();result.add(tempNode.val);current = tempNode.right;}}return result;} 
}

 后序遍历-力扣145,后序遍历可以当成是把前序遍历顺序改变一下,从前序的中左右变成中右左,然后再把结果倒置。

class Solution {//后序遍历public List<Integer> postorderTraversal(TreeNode root) {List<Integer> result = new LinkedList<>();if (root == null) {return result;}Deque<TreeNode> stack = new LinkedList<>();stack.push(root);TreeNode cur = root;while (!stack.isEmpty()) {TreeNode tempNode = stack.pop();result.add(tempNode.val);if (tempNode.left != null) {stack.push(tempNode.left);}if (tempNode.right != null) {stack.push(tempNode.right);}}Collections.reverse(result);return result;}}
http://www.lryc.cn/news/106259.html

相关文章:

  • 什么是线程?为什么需要线程?和进程的区别?
  • 【业务功能篇61】SpringBoot项目流水线 dependencyManagement 标签整改依赖包版本漏洞问题
  • uniapp使用getStorage对属性赋值无效
  • 20230802-下载并安装android-studio
  • python 第九章 —— GUI界面开发(tkinter详解)
  • 线段树合并例题
  • Stable Diffusion 硬核生存指南:WebUI 中的 VAE
  • vue项目 前端加前缀(包括页面及静态资源)
  • 使用文心一言等智能工具指数级提升嵌入式/物联网(M5Atom/ESP32)和机器人操作系统(ROS1/ROS2)学习研究和开发效率
  • 【Rust 基础篇】Rust动态大小类型:理解动态大小类型与编写安全的代码
  • 【Python】使用nuitka打包Python程序为EXE可执行程序
  • 背景图片及精灵图
  • 简要介绍 | 生成模型的演进:从自编码器(AE)到变分自编码器(VAE)和生成对抗网络(GAN),再到扩散模型
  • 8.2Thread类的常见属性
  • 博客摘录「 mvvm框架工作原理及优缺」2023年7月31日
  • 第12章 Linux 实操篇-Linux磁盘分区、挂载
  • 使用express搭建后端服务
  • 深度学习——划分自定义数据集
  • Jmeter性能测试之正则表达式提取器
  • 浅谈Kubernetes中Service网络实现(服务发现)
  • 【重造轮子】golang实现可重入锁
  • torch显存分析——对生成模型清除显存
  • electron+vue+ts窗口间通信
  • 基于Fringe-Projection环形投影技术的人脸三维形状提取算法matlab仿真
  • 如何使用Webman框架实现多语言支持和国际化功能?
  • 接受平庸,特别是程序员
  • HTML兼容性
  • Java日期和时间处理入门指南
  • anndata k折交叉
  • 深入解析项目管理中的用户流程图