二叉树的遍历

二叉树用例

代码解析:

public class BinaryTree {

    static class TreeNode {
Integer val;
TreeNode left;
TreeNode right; public TreeNode(Integer val) {
this.val = val;
}
} public static TreeNode init(Integer[] arr, int index) {
TreeNode node = null;
if (index < arr.length) {
node = new TreeNode(arr[index]);
node.left = init(arr, 2 * index + 1);
node.right = init(arr, 2 * index + 2);
}
return node;
} private static List<Integer> list = new ArrayList<>(10); public static void main(String[] args) {
Integer[] arr = new Integer[]{1, 3, 4, 5, 6, 7, 8}; System.out.println("递归实现前序遍历: "+ rootLeftRightRecursive(init(arr,0)));
list.clear();
System.out.println("非递归实现前序遍历: "+ rootLeftRightNonRecursive(init(arr,0)));
list.clear(); System.out.println(); System.out.println("递归实现中序遍历: "+ leftRootRightRecursive(init(arr,0)));
list.clear();
System.out.println("非递归实现中序遍历: "+ leftRootRightNonRecursive(init(arr,0)));
list.clear(); System.out.println(); System.out.println("递归实现后序遍历: "+ leftRightRootRecursive(init(arr,0)));
list.clear();
System.out.println("非递归实现后序遍历: "+ leftRightRootNonRecursive(init(arr,0)));
list.clear(); System.out.println(); System.out.println("层次遍历: "+ levelOrder(init(arr,0))); System.out.println(); System.out.println("树的深度为: "+ depth(init(arr,0))); } /**
* 递归实现前序遍历
* 中-左-右
* @param node TreeNode
* @return List
*/
public static List rootLeftRightRecursive(TreeNode node) {
if (null != node){
list.add(node.val);
rootLeftRightRecursive(node.left);
rootLeftRightRecursive(node.right);
}
return list;
} /**
* 非递归实现前序遍历
* 中-左-右
* @param node TreeNode
* @return List
*/
public static List rootLeftRightNonRecursive(TreeNode node) {
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = node; while (null != cur || !stack.isEmpty()) {
if (null != cur) {
list.add(cur.val);
stack.push(cur);
cur = cur.left; } else {
cur = stack.pop();
cur = cur.right;
}
}
return list;
} /**
* 递归实现中序遍历
* 左-中-右
* @param node TreeNode
* @return List
*/
public static List leftRootRightRecursive(TreeNode node) {
if (null!=node){
leftRootRightRecursive(node.left);
list.add(node.val);
leftRootRightRecursive(node.right);
}
return list;
} /**
* 非递归实现中序遍历
* 左-中-右
* @param node TreeNode
* @return List
*/
public static List leftRootRightNonRecursive(TreeNode node) {
List<Integer> list = new ArrayList<>(10);
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = node; while (null != cur || !stack.isEmpty()) {
if (null != cur) {
stack.push(cur);
cur = cur.left;
} else {
cur = stack.pop();
list.add(cur.val);
cur = cur.right;
}
} return list;
} /**
* 递归实现后序遍历
* 左-右-中
* @param node TreeNode
* @return List
*/
public static List leftRightRootRecursive(TreeNode node){ if (null!=node){
leftRightRootRecursive(node.left);
leftRightRootRecursive(node.right);
list.add(node.val);
}
return list;
} /**
* 非递归实现后序遍历
* 左-右-中
* @param node TreeNode
* @return List
*/
public static List leftRightRootNonRecursive(TreeNode node){
if (null == node){
return list;
}
Stack<TreeNode> stack = new Stack<>();
stack.push(node);
TreeNode cur; while (!stack.isEmpty()){
cur = stack.pop();
if (cur.left!=null){
stack.push(cur.left);
}
if (cur.right!=null){
stack.push(cur.right);
}
// 逆序添加
list.add(0,cur.val);
}
return list;
} /**
* 层序遍历队列实现(广度优先算法BFS)
* @param root TreeNode
* @return List
*/
public static List<List<Integer>> levelOrder(TreeNode root){
List<List<Integer>> list = new ArrayList<>();
if(root == null){
return list;
} Queue<TreeNode> queue = new LinkedList<>();
queue.add(root); while(!queue.isEmpty()){
int count = queue.size();
List<Integer> tmpList = new ArrayList<>();
while(count > 0){
TreeNode node = queue.poll();
tmpList.add(node.val);
if(node.left!=null){
queue.add(node.left);
}
if(node.right!=null){
queue.add(node.right);
}
count--;
}
list.add(tmpList);
}
return list;
} /**
* 递归实现获取树的深度
* @param node TreeNode
* @return int
*/
public static int depth(TreeNode node){
if (node == null){
return 0;
}
int left = depth(node.left);
int right = depth(node.right); return left > right ? left + 1 : right + 1;
} }

结果为:

递归实现前序遍历:   [1, 3, 5, 6, 4, 7, 8]
非递归实现前序遍历: [1, 3, 5, 6, 4, 7, 8] 递归实现中序遍历: [5, 3, 6, 1, 7, 4, 8]
非递归实现中序遍历: [5, 3, 6, 1, 7, 4, 8] 递归实现后序遍历: [5, 6, 3, 7, 8, 4, 1]
非递归实现后序遍历: [5, 6, 3, 7, 8, 4, 1] 层次遍历: [[1], [3, 4], [5, 6, 7, 8]] 树的深度为: 3

【algorithm】二叉树的遍历的更多相关文章

  1. 二叉树的遍历(递归,迭代,Morris遍历)

    二叉树的三种遍历方法: 先序,中序,后序,这三种遍历方式每一个都可以用递归,迭代,Morris三种形式实现,其中Morris效率最高,空间复杂度为O(1). 主要参考博客: 二叉树的遍历(递归,迭代, ...

  2. C++ 二叉树深度优先遍历和广度优先遍历

    二叉树的创建代码==>C++ 创建和遍历二叉树 深度优先遍历:是沿着树的深度遍历树的节点,尽可能深的搜索树的分支. //深度优先遍历二叉树void depthFirstSearch(Tree r ...

  3. 二叉树的遍历(递归,迭代,Morris遍历)

    二叉树的遍历: 先序,中序,后序: 二叉树的遍历有三种常见的方法, 最简单的实现就是递归调用, 另外就是飞递归的迭代调用, 最后还有O(1)空间的morris遍历: 二叉树的结构定义: struct ...

  4. [Leetcode] Binary tree level order traversal二叉树层次遍历

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  5. 算法与数据结构(三) 二叉树的遍历及其线索化(Swift版)

    前面两篇博客介绍了线性表的顺序存储与链式存储以及对应的操作,并且还聊了栈与队列的相关内容.本篇博客我们就继续聊数据结构的相关东西,并且所涉及的相关Demo依然使用面向对象语言Swift来表示.本篇博客 ...

  6. C++版 - 剑指Offer 面试题39:二叉树的深度(高度)(二叉树深度优先遍历dfs的应用) 题解

    剑指Offer 面试题39:二叉树的深度(高度) 题目:输入一棵二叉树的根结点,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度.例如:输入二叉树 ...

  7. python3实现二叉树的遍历与递归算法解析

    1.二叉树的三种遍历方式 二叉树有三种遍历方式:先序遍历,中序遍历,后续遍历  即:先中后指的是访问根节点的顺序   eg:先序 根左右   中序 左根右  后序  左右根 遍历总体思路:将树分成最小 ...

  8. 二叉树的遍历--C#程序举例二叉树的遍历

    二叉树的遍历--C#程序举例二叉树的遍历 关于二叉树的介绍笨男孩前面写过一篇博客 二叉树的简单介绍以及二叉树的存储结构 遍历方案 二叉树的遍历分为以下三种: 先序遍历:遍历顺序规则为[根左右] 中序遍 ...

  9. 数据结构与算法之PHP实现二叉树的遍历

    一.二叉树的遍历 以某种特定顺序访问树中所有的节点称为树的遍历,遍历二叉树可分深度优先遍历和广度优先遍历. 深度优先遍历:对每一个可能的分支路径深入到不能再深入为止,而且每个节点只能访问一次.可以细分 ...

随机推荐

  1. 数据摘要pandas

    主要是用于分析数据的Pandas库 先学习两个数据类型DataFrame和series 进一步学习利用Pandas进行摘要的方法, 提取数据的特征 1 pandas库 1.1 pandas库 pand ...

  2. 微信小程序自定义下导航页面切换效果的合理写法

    上图::: 导航模板内容页面的定义: <template name="naviBot">   <view class='navwrap t_cen font_26 ...

  3. [SDOI2012]任务安排

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2726 [算法] 此题与POJ1180非常相似 但是 , 此题中的t值可能为负 , 这 ...

  4. virtualBox中的centOS虚拟机硬盘扩容

    1. 在virtualBox中给虚拟机添加虚拟硬盘 此时. 已经将yanwu_disk1.vdi 虚拟硬盘添加到了虚拟机中, 接下来就是进行硬盘的挂载 https://www.cnblogs.com/ ...

  5. DispatcherServlet详解

    1.1.DispatcherServlet作用 DispatcherServlet是前端控制器设计模式的实现,提供Spring Web MVC的集中访问点,而且负责职责的分派,而且与Spring Io ...

  6. python--flask学习1

    1 windows/unix得安装 http://www.pythondoc.com/flask-mega-tutorial/helloworld.html http://www.pythondoc. ...

  7. TypeScript完全解读(26课时)_18.Mixins混入

    本节的代码在mixin.ts文件内 同时在index.ts内引入 混入就是把两个对象或者类的内容混合到一起,从而实现一些功能复用. 对象混入 js中对象的混入 先来看一个js中对象的混入的例子 首先定 ...

  8. mysql添加DATETIME类型字段导致Invalid default value错误的问题

    例如: CREATE TABLE foo ( `creation_time` DATETIME DEFAULT CURRENT_TIMESTAMP, `modification_time` DATET ...

  9. STL中的vector实现邻接表

    /* STL中的vector实现邻接表 2014-4-2 08:28:45 */ #include <iostream> #include <vector> #include  ...

  10. SCUT - 114 - 作业之数学篇 - 杜教筛

    https://scut.online/p/114 \(A(n)=\sum\limits_{i=1}^{n} \frac{lcm(i,n)}{gcd(i,n)}\) \(=\sum\limits_{i ...