二叉树的遍历

二叉树用例

代码解析:

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. 「NOIP2014」「Codevs3728」 联合权值(乱搞

    3728 联合权值 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold   题目描述 Description 输入描述 Input Description 输出描述 Ou ...

  2. Azure SQL Database (27) 创建Table Partition

    <Windows Azure Platform 系列文章目录> 昨天客户正好提到这个问题,现在记录一下. 我们在使用传统的SQL Server,会使用Table Partition,这个功 ...

  3. python_re函数

    1,贪婪和非贪婪模式 重复运算符默认是贪婪的,即会进行尽可能多的匹配 代码示例: >>> import re >>> emphasis_pattern = re.c ...

  4. 第一篇:构建第一个SpringBoot工程

    简介 spring boot 它的设计目的就是为例简化开发,开启了各种自动装配,你不想写各种配置文件,引入相关的依赖就能迅速搭建起一个web工程.它采用的是建立生产就绪的应用程序观点,优先于配置的惯例 ...

  5. UVA - 11624 Fire! 双向BFS追击问题

    Fire! Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of ...

  6. JAVA基础--JAVA API集合框架(其他集合类,集合原理)15

    一.ArrayList介绍 1.ArrayList介绍 ArrayList它是List接口的真正的实现类.也是我们开发中真正需要使用集合容器对象. ArrayList类,它是List接口的实现.肯定拥 ...

  7. python-codecs.open()使用举例

    代码: import codecs from unidecode import unidecode def main(): fullFilename="123.txt" inFID ...

  8. [Xcode 实际操作]九、实用进阶-(25)使用Storyboard(故事版)的约束功能,使项目快速适配各种分辨率的设备

    目录:[Swift]Xcode实际操作 本文将演示使用故事版的约束功能,使项目快速适配各种分辨率的设备. 在项目导航区打开并编辑主故事版[Main.storyboard]. 在当前故事版中,已经存在一 ...

  9. SpringBoot2.0 基础案例(02):配置Log4j2,实现不同环境日志打印

    一.Log4j2日志简介 日志打印是了解Web项目运行的最直接方式,所以在项目开发中是需要首先搭建好的环境. 1.Log4j2特点 1)核心特点 相比与其他的日志系统,log4j2丢数据这种情况少:d ...

  10. 在 UIViewController 中手动增加 TableView 出现 Type 'SomeViewController' does not confirm to protocol 'UITableViewDataSource' 问题的解决办法

    许多时候我们都有在普通的继承自 UIViewController 的控制器中使用 TableView 的需求,这时候就需要当前控制器类继承 UITableViewDelegate 和 UITableV ...