【algorithm】二叉树的遍历
二叉树的遍历
二叉树用例

代码解析:
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】二叉树的遍历的更多相关文章
- 二叉树的遍历(递归,迭代,Morris遍历)
二叉树的三种遍历方法: 先序,中序,后序,这三种遍历方式每一个都可以用递归,迭代,Morris三种形式实现,其中Morris效率最高,空间复杂度为O(1). 主要参考博客: 二叉树的遍历(递归,迭代, ...
- C++ 二叉树深度优先遍历和广度优先遍历
二叉树的创建代码==>C++ 创建和遍历二叉树 深度优先遍历:是沿着树的深度遍历树的节点,尽可能深的搜索树的分支. //深度优先遍历二叉树void depthFirstSearch(Tree r ...
- 二叉树的遍历(递归,迭代,Morris遍历)
二叉树的遍历: 先序,中序,后序: 二叉树的遍历有三种常见的方法, 最简单的实现就是递归调用, 另外就是飞递归的迭代调用, 最后还有O(1)空间的morris遍历: 二叉树的结构定义: struct ...
- [Leetcode] Binary tree level order traversal二叉树层次遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- 算法与数据结构(三) 二叉树的遍历及其线索化(Swift版)
前面两篇博客介绍了线性表的顺序存储与链式存储以及对应的操作,并且还聊了栈与队列的相关内容.本篇博客我们就继续聊数据结构的相关东西,并且所涉及的相关Demo依然使用面向对象语言Swift来表示.本篇博客 ...
- C++版 - 剑指Offer 面试题39:二叉树的深度(高度)(二叉树深度优先遍历dfs的应用) 题解
剑指Offer 面试题39:二叉树的深度(高度) 题目:输入一棵二叉树的根结点,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度.例如:输入二叉树 ...
- python3实现二叉树的遍历与递归算法解析
1.二叉树的三种遍历方式 二叉树有三种遍历方式:先序遍历,中序遍历,后续遍历 即:先中后指的是访问根节点的顺序 eg:先序 根左右 中序 左根右 后序 左右根 遍历总体思路:将树分成最小 ...
- 二叉树的遍历--C#程序举例二叉树的遍历
二叉树的遍历--C#程序举例二叉树的遍历 关于二叉树的介绍笨男孩前面写过一篇博客 二叉树的简单介绍以及二叉树的存储结构 遍历方案 二叉树的遍历分为以下三种: 先序遍历:遍历顺序规则为[根左右] 中序遍 ...
- 数据结构与算法之PHP实现二叉树的遍历
一.二叉树的遍历 以某种特定顺序访问树中所有的节点称为树的遍历,遍历二叉树可分深度优先遍历和广度优先遍历. 深度优先遍历:对每一个可能的分支路径深入到不能再深入为止,而且每个节点只能访问一次.可以细分 ...
随机推荐
- python 基础之第八天--字典相关
zx #####################创建字典###################################### In [11]: dict([('name','bob'),('a ...
- CTSC2012 熟悉的文章
传送门 首先很容易想到对于所有的模式串建出广义后缀自动机,之后对于我们每一个要检查的文本串,先在SAM上跑,计算出来每一个位置能匹配到的最远的位置是多少.(就是当前点减去匹配长度) 之后--考虑DP- ...
- Azure Key Vault (1) 入门
<Windows Azure Platform 系列文章目录> 为什么要使用Azure Key Vault? 我们假设在微软云Azure上有1个场景,在Windows VM里面有1个.NE ...
- ubuntu12.04下有线网无电缆插入问题
解决方案: 1.查看是否没装网卡驱动: 2.检查网卡是否损坏: 3.检查网线是否损坏(注意:有时候不一定是网线损坏了,而是网线太细了,导通性不好.本人用细网线试了一下,windows下面可以连接网络, ...
- Qt .pro文件配置大全!
避免以后的无意义重复劳动,将用过的所有的头文件库文件的配置都放在这里,以后要用的话直接copy就好. eigen3: INCLUDEPATH += \ /usr/local/include/eigen ...
- js的call()通俗解释
var x = "我是全局变量"; //定义全局变量x function a(){ //定义函数类结构a this.x = "我是在函数类结构a中声明的哦"; ...
- python之文件的读写(1)
真的崩溃,刚写完的笔记由于点错了,现在特么又要重新写了. 崩溃呀.......... 之前的废话就不再重复了,直接进入正题吧. 今天小R 学了一天的NP课程,但是python还是不能忘得,所以晚上又 ...
- [CVE-2017-8464]Microsoft Windows远程命令执行漏洞复现
版权声明:本文为博主的原创文章,未经博主同意不得转载 前言 记录下自己的复现,保留意见 2017年6月13日,微软官方发布编号为CVE-2017-8464的漏洞公告,官方介绍Windows系统在解析快 ...
- 设置android设备时间与pc时间同步的批处理
新建一个批处理文件 然后输入下面的内容: @echo off echo %date% echo %time% ,%%,%%,%.%,%%,%%,% //通过获取pc时间来设置android设备时间 a ...
- 201621123016 《Java程序设计》第十四周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结与数据库相关内容. 使用数据库技术改造你的系统 2.1 简述如何使用数据库技术改造你的系统.要建立什么表?截图你的表设计. 把我原来 ...