树的几种遍历方式(递归/非递归)
树的几种遍历方式,前序遍历,中序遍历,后序遍历,包括它的递归实现以及非递归实现
非递归遍历
-----------------前序遍历------------------------
class Solution {public List<Integer> preorderTraversal(TreeNode root) {List<Integer> res = new ArrayList<>();if(root == null) return res;Stack<TreeNode> stack = new Stack<>();stack.push(root);while(!stack.isEmpty()){TreeNode node = stack.pop();res.add(node.val);if(node.right != null) stack.push(node.right);if(node.left != null) stack.push(node.left);}return res;}
}-----------后序遍历-----------------------------class Solution {public List<Integer> postorderTraversal(TreeNode root) {List<Integer> res = new ArrayList<>();if(root == null) return res;Deque<TreeNode> deque = new LinkedList<>();deque.push(root);while(!deque.isEmpty()){TreeNode node = deque.pop();res.add(node.val);if(node.left != null) deque.push(node.left);if(node.right != null) deque.push(node.right);}Collections.reverse(res);return res;}
}
另一种方法
class Solution {public List<Integer> postorderTraversal(TreeNode root) {List<Integer> res = new ArrayList<Integer>();if (root == null) {return res;}Deque<TreeNode> stack = new LinkedList<TreeNode>();TreeNode prev = null;while (root != null || !stack.isEmpty()) {while (root != null) {stack.push(root);root = root.left;}root = stack.pop();if (root.right == null || root.right == prev) {res.add(root.val);prev = root;root = null;} else {stack.push(root);root = root.right;}}return res;}
}
前序非递归:
public List<Integer> preTravel(TreeNode root){List<Integer> res = new ArrayList<>();if(root == null) return res;Stack<TreeNode> stack = new Stack<>();while(!stack.isEmpty() || root != null){while(root != null){res.add(root.val);stack.push(root);root = root.left;}if(!stack.isEmpty()){root = stack.pop();root = root.right;}}return res;}
中序非递归:
public List<Integer> midTravel(TreeNode root){List<Integer> res = new ArrayList<>();if(root == null) return res;Stack<TreeNode> stack = new Stack<>();while(!stack.isEmpty() || root != null){while(root != null){stack.push(root);root = root.left;}if(!stack.isEmpty()){root = stack.pop();res.add(root.val);root = root.right;}}return res;}
树的几种遍历方式(递归/非递归)相关推荐
- 二叉树三种遍历方式的非递归实现
树的递归实现方式很简单,下面介绍三种遍历的非递归实现. 树的遍历有个特点,那就是在处理具体问题时,绝大多数情况下是在当前循环.或函数(或是子树)的根节点来处理的,能够注意到当前根节点是如何从上个根节点 ...
- 二分查找算法的两种实现方式:非递归实现和递归实现
二分查找的条件是对一组有序数组的查找,这一点很容易忘记,在使用二分查找的时候先要对数组进行排序. 先说一下二分查找的思路:一个有序数组,想要查找一个数字key的下标,首先算出中间下标mid,利用mid ...
- 二叉树几种遍历算法的非递归实现
二叉树遍历的非递归实现 相对于递归遍历二叉树,非递归遍历显得复杂了许多,但换来的好处是算法的时间效率有了提高.下面对于我学习非递归遍历二叉树算法的过程进行总结 为了便于理解,这里以下图的二叉树为例,分 ...
- 常用数据结构之二叉树及树的四种遍历方式
1.树 我们选择一种数据结构,不仅要能存储数据,而且要能体现数据之间的关系.目前数据主要有是三种关系一对一.一对多.多对多:之前我们讨论了线性表(数组.链表.栈.队列),其中的元素具有一对一的关系,通 ...
- 按照前序遍历创建二叉树及树的四种遍历方式
一.二叉树的介绍 二叉树的特点是二叉树的每个结点的度都不大于2,可以视为每个结点都有左孩子和右孩子.故二叉树结点的数据结构为 二.二叉树的特点 1.设根结点所在的层数为第1层,则第i层最多有个结点. ...
- c语言中二叉树中总结点,C语言二叉树的三种遍历方式的实现及原理
二叉树遍历分为三种:前序.中序.后序,其中序遍历最为重要.为啥叫这个名字?是根据根节点的顺序命名的. 比如上图正常的一个满节点,A:根节点.B:左节点.C:右节点,前序顺序是ABC(根节点排最先,然后 ...
- c语言二叉树的遍历菜单系统,C语言二叉树的三种遍历方式的实现及原理
C语言二叉树的三种遍历方式的实现及原理 发布时间:2020-10-03 19:43:57 来源:脚本之家 阅读:63 作者:看雪. 二叉树遍历分为三种:前序.中序.后序,其中序遍历最为重要.为啥叫这个 ...
- python数据结构与算法:二叉树及三种遍历方式(先序遍历/中序遍历/后序遍历)
树的实现采用queue的形式: 树的三种遍历方式(广度优先白能力法):先序遍历(根左右),中序遍历(左根右)以及后序遍历(左右根) ######################P6.4 数据结构### ...
- 树的三种遍历原理及实现
树的三种遍历 一棵树的三种遍历方式:先序遍历,中序遍历,后序遍历. 前中后三种顺序其实指的是根的顺序. 先序遍历:根节点.左子树.右子树 中序遍历:左子树.根节点.右子树 后序遍历:左子树.右子树.根 ...
最新文章
- Windows 7 RC Build 7100 使用报告
- 镜像的分层结构 - 每天5分钟玩转容器技术(11)
- mysql 存储过程乱码的问题
- TensorFlow for Hackers (Part VI) - Human Activity Recognition using LSTMs on Android
- Matlab的Floor, Ceil, Fix, Round
- 【solr专题之四】关于VelocityResponseWriter
- vfp程序转换为c语言程序软件,c语言程序设计及vfp程序设计试题.doc
- 为Visual Studio 2010增加ExtJs智能提示
- python 用户的画像可视化呈现技术_一人一车一面:解读汽车大数据用户画像背后的AI技术...
- RabbitMQ详解(一)
- LINQ-to-SQL那点事~耳目一新的partial class 和 partial method
- 技术圈儿001---分布式事务最经典的七种解决方案
- 手把手教你如何下载编译Spring源码
- CDN技术详解(电子书)下载链接
- shell编程三大神器之sed
- 5.图像,音视频标签
- 详解PON基础知识:OLT、ONU、ONT和ODN
- Cython 的简要入门、编译及使用
- 洛谷P3933 Chtholly Nota Seniorious 【二分 + 贪心 + 矩阵旋转】
- 数据库——sql-server