Morris InOrder Traverse Binary Tree 无需使用递归和栈
今天在切leetcode的时候看到一个Morris算法,用来中序遍历二叉树,非递归,O(1)空间。觉得很强大。记录一下。
基本思想是利用了Threaded Binary Tree。
步骤如下:
- current节点设置为root。如果current不为空,到2,否则返回;
- 如果current没有左子树,输出current的值,current等于current.right;
- 如果current有左子树,首先找到current节点的precedent,也就是该节点左子树中最最右边那个节点。然后把最最右边这个节点的右link指向当前节点。如下图。

e.g. 当current是7的时候,我们找到4,并人为地添加一个link到current(绿色的link)。
current等于current.left;回到2.
有同学说,如果遍历到结点4,按照算法(4没有左子树),不是就又回到了7么,然后循环怎么结束呢?假设如果通过4回到了7,再找寻找7的precendent的过程中,我们会发现环,7->3->4->7(7的左子树中最最右边的节点是自己),那么我们知道7的左子树已经遍历完成,输出7,然后继续遍历7的右子树。
我们的代码如下:
首先假设有一个TreeNode数据结构是这样的。
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
然后是遍历:
public ArrayList<Integer> inorderMorrisTraversal(TreeNode root){
sequence = new ArrayList<Integer>();
TreeNode current = root;
TreeNode pre = null;
while(current != null){
if(current.left == null){
sequence.add(current.val);
current = current.right;
}else {
pre = current.left;
//找到当前节点的前任,也就是它左子树的最右节点
while(pre.right != null && pre.right != current){
pre = pre.right;
}
if(pre.right == null){//我们遇到的左子树
pre.right = current;
current = current.left;
}else {//说明pre.right == current,构成了一个环,说明之前已经遍历过了current的左子树,可以输出current了。
pre.right = null;
sequence.add(current.val);
current = current.right;
}
}
}
return sequence;
我们以上面的例子track一下这个过程,首先current指向root节点7. root节点左子树非空,通过一路向右,找到7的前任4,建立绿色的link。

然后继续到3,在左子树中一路向右,找到2.

继续current = current.left,发现2没有左子树了,输出2.

然后current = current.right,current指向3. 注意到这是第二次指向3. 然后按照算法去寻找3的前任,当然这一回就不是2了,而是3本身。那么,我们需要删除掉这个环,也就2->3的link。并且输出current 的值3.

然后继续current到3的左子树。剩下的过程如下图。

总结下:
首先发明这个算法的人肯定是对那个什么Threaded Binary Tree烂熟于心啊;其次,对inorder遍历也是理解透彻啊。。。
再次,这人思维肯定特清晰。
Reference: http://www.geeksforgeeks.org/inorder-tree-traversal-without-recursion-and-without-stack/
Morris InOrder Traverse Binary Tree 无需使用递归和栈的更多相关文章
- CSharp Algorithm - How to traverse binary tree by breadth (Part II)
/* Author: Jiangong SUN */ Here I will introduce the breadth first traversal of binary tree. The pri ...
- Post Order traverse binary tree using non-recursive way
Process analysis Stack = 5, Push 3, Stack = 5, 3. Pre = 5 Current = 3, Pre = 5, Push 2 to the st ...
- [LeetCode] Binary Tree Preorder Traversal 二叉树的先序遍历
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- [Algorithm] Construct a Binary Tree and Binary Search
function createNode(value) { return { value, left: null, right: null }; } function BinaryTree(val) { ...
- [LeetCode] 145. Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- [Leetcode][JAVA] Recover Binary Search Tree (Morris Inorder Traversal)
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- (二叉树 递归) leetcode94. Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...
- (二叉树 递归) leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
随机推荐
- Linux 设置 LD_LIBRARY_PATH
在Linux下,如果自己写好一个动态链接库,需要在其他程序里调用,则需要让这些程序能找到这个动态链接库,如果设置的不对,就会出现类似的错误: error : cannot open shared ob ...
- Spring 4 官方文档学习(十)数据访问之DAO支持
1.介绍 Spring 中 Data Access Object (DAO)支持 的目标是以一种一致的方式更简单的使用JDBC.Hibernate.JPA或JDO等数据访问技术.可以在前面说的几种数据 ...
- nodejs基础 -- 模块系统
为了让nodejs的文件可以相互调用,nodejs提供了一个简单的模块系统. 模块:是nodejs应用程序的基本组成部分,文件和模块一一对应.即,一个nodejs文件就是一个模块,这个文件可能是jav ...
- circRNA 序列提取中的难点
在预测circRNA时,都是检测breakpoint 处的reads 数,最后给出的环状RNA的ID 都是诸如 chr14:106994222-107183708 这样的形式,给出了起始和终止位置: ...
- Python中tab键自动补全功能的配置
新手学习Python的时候,如何没有tab键补全功能,我感觉那将是一个噩梦,对于我们这种菜鸟来说,刚接触python,对一切都不了解,还好有前辈们的指导,学习一下,并记录下来,还没有学习这个功能小伙伴 ...
- 记录一下我的GDB配置
一:为了更好的在GDB中显示STL容器.我们首先要下载一个python脚本 PS:要确定你所安装的GDB能够运行python脚本 cd ~ mkdir .gdb cd .gdb svn co svn: ...
- 扒一扒MathType不为人知的技巧
MathType作为一款编辑数学公式的神器,很多人在使用它时只是很简单地使用了一些最基本的模板,很多功能都没有使用.MathType功能比你想象中的大很多,今天我们就来扒一扒MathType那些不为人 ...
- 上千万或上亿数据(有反复),统计当中出现次数最多的N个数据. C++实现
上千万或上亿的数据,如今的机器的内存应该能存下.所以考虑採用hash_map/搜索二叉树/红黑树等来进行统计次数. 然后就是取出前N个出现次数最多的数据了,能够用第2题提到的堆机制完毕. #inclu ...
- 分布式消息队列RocketMQ与Kafka架构上的巨大差异之1 -- 为什么RocketMQ要去除ZK依赖?
我们知道,在早期的RocketMQ版本中,是有依赖ZK的.而现在的版本中,是去掉了对ZK的依赖,转而使用自己开发的NameSrv. 并且这个NameSrv是无状态的,你可以随意的部署多台,其代码也非常 ...
- 安装autoit libary失败问题解决
运行环境:win7-32,python2.7安装遇到的问题: