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 ...
随机推荐
- Firefox 在LR录制过程中添加例外的问题解决方法
用lr调火狐打开网页 会报证书安全问题 证书安全提示目的是告诉你这个服务器使用的证书可能不安全,要不要信任,你自己决定,不信任就不能访问.为什么会报证书安全,因为浏览器没添加该证书.或者由于性能工具 ...
- HBase系统入门--整体介绍
转自:http://www.aboutyun.com/thread-8957-1-2.html 问题导读:1.HBase查询与写入哪个更好一些?2.HBase面对复杂操作能否实现?3.Region服务 ...
- 为什么要把session存入数据库
比如网易的通行证,一个session能进入很多的网易下的网站
- 关于Cocos2d-x程序运行时候提示关闭程序的框框的解决方法
1.这个情况是资源没有被加载的表现 如果AppDelegate.cpp里面没有文件索引的语句 FileUtils::getInstance()->addSearchPath("res& ...
- 获取platformVersion、deviceName、appPackage
命令获取如下: 手机与电脑连接 devicename: adb devices platformversion : adb shell getprop ro.build.version.re ...
- 选择列表中的列……无效,因为该列没有包含在聚合函数或 GROUP BY 子句中
今天用SQL Server尝试实现一个SQL语句的时候,报了如标题所示的错误,通过在百度里面搜索,并亲自动手实现,终于发现问题所在,现在把它记录下来. 语句如下: select [OrderID],[ ...
- 回调方法介绍之中国好室友篇(Java示例)
前言 在Java社区的各种开源工具中,回调方法的使用俯拾即是.所以熟悉回调方法无疑能加速自己对开源轮子的掌握.网上搜了一些文章,奈何对回调方法的介绍大多只停留在什么是回调方法的程度上.本篇文章尝试从回 ...
- sendEvent()
sendEvent()(QObject *receiver, QEvent *event)可以将自己创建事件event发送给制定的receiver 例如, //创建按键事件 QKeyEvent key ...
- 第三百一十二节,Django框架,Cookie
第三百一十二节,Django框架,Cookie 注意:获取Cookie是在请求对象里处理,设置Cookie是在响应对象里处理 普通Cookieset_cookie()设置普通cookie 参数: ke ...
- 有关JSP隐式对象,以下( )描述正确。
A.隐式对象是WEB容器加载的一组类的实例,可以直接在JSP页面使用 B.不能通过config对象获取ServletContext对象 C.response对象通过sendRedirect方法实现重定 ...