【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实现二叉树的遍历
一.二叉树的遍历 以某种特定顺序访问树中所有的节点称为树的遍历,遍历二叉树可分深度优先遍历和广度优先遍历. 深度优先遍历:对每一个可能的分支路径深入到不能再深入为止,而且每个节点只能访问一次.可以细分 ...
随机推荐
- nltk: Tokenizing text into sentences
安装 nltk pip install nltk 下载nltk_data 方法一: 通过客户端下载 import nltk nltk.download() 出现如下客户端,选择所需的包下载.(但由于网 ...
- java 02 内部类
- android开发中怎么通过Log函数输出当前行号和当前函数名
public class Debug { public static int line(Exception e) { StackTraceElement[] trace = e.getStackTra ...
- bzoj2718
二分图匹配 首先有个定理:最长反链=最小链覆盖 最小链覆盖可以重复经过点 所以我们不能直接建图 那么我们用floyd判断是否相连 然后建图就行了 #include<bits/stdc++.h&g ...
- CodeForces 1110H. Modest Substrings
题目简述:给定$1 \leq l \leq r \leq 10^{800}$,求一个长度为$n \leq 2000$的数字串$s$,其含有最多的[好]子串.一个串$s$是[好]的,如果将其看做数字时无 ...
- Qt .pro文件配置大全!
避免以后的无意义重复劳动,将用过的所有的头文件库文件的配置都放在这里,以后要用的话直接copy就好. eigen3: INCLUDEPATH += \ /usr/local/include/eigen ...
- 01 mybatis框架整体概况(2018.7.10)-
01 mybatis框架整体概况(2018.7.10)- F:\廖雪峰 JavaEE 企业级分布式高级架构师课程\廖雪峰JavaEE一期\第一课(2018.7.10) maven用的是3.39的版本 ...
- IE8 以上版本兼容
在html的内如下写法 其中最后一行是永远以最新的IE版本模式来显示网页的. 另外加上Emulate模式 Emulate模式后则更重视 (细心的人会注意到,用IE9去访问带有x-ua-compatib ...
- Codeforces325 D【并查集维护连通性】
参考:大牛blog 思路: 因为是环,所以可以复制一下图,先判断一下和他是不是和与他相邻的8个之一的一个障碍使得构成了一个环,环就是一个连通,用并查集维护即可: 如果没有就ans++,然后并把这个点加 ...
- jzoj5988. 【WC2019模拟2019.1.4】珂学计树题 (burnside引理)
传送门 题面 liu_runda曾经是个喜欢切数数题的OIer,往往看到数数题他就开始刚数数题.于是liu_runda出了一个数树题.听说OI圈子珂学盛行,他就在题目名字里加了珂学二字.一开始liu_ ...