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 ...
随机推荐
- thinkphp 3.2跟3.1 区别
1.ThinkPHP3.2 主要整合了3.1以及之前版本的分组问题.3.2之前有普通分组和独立分组,还需要在配置文件中配置.3.2已经不需要配置了,直接使用独立分组就可以了.2.ThinkPHP3.2 ...
- 关于Cocos2d-x运行项目时弹框崩溃的解决
想要运行工程的时候,跳出一个框说停止cantnot open the window,还提到什么GLVM之类的,这是显卡驱动出现问题,如果是远程连接电脑的话,很有可能就是用来远程连接的那台电脑的显卡驱动 ...
- linux -- 注销,关机,重启
注销:logout Logout 注销是登陆的相对操作,登陆系统后,若要离开系统,用户只要直接下达logout命令即可: [root@localhost root]#logout Red Hat ...
- AsyncTask执行顺序
这几天,遇见个奇葩问题,记录一下. 在用AsyncTask的时候,new 出来的AsyncTask总是等了很久才执行到. 于是乎,想到了是不是前面已经有好几个AsyncTask的实例了,是不是线程优先 ...
- Qt模态与非模态
模态对话框就是指在子对话框弹出时,焦点被强行集中于该子对话框,子对话框不关闭,用户将无法操作其他的窗口.非模态相反,用户仍然可以操作其他的窗口,包括该子对话框的父对话框. 如果从线程角度来讲,模态对话 ...
- php跨form提交方法
1.php curl function curlPost($url,$params) { $postData = ''; foreach($params as $k => $v) { $post ...
- 解决 SharePoint 2010 拒绝访问爬网内容源错误的小技巧(禁用环回请求的两种方式)
这里有一条解决在SharePoint 2010搜索爬网时遇到的“拒绝访问错误”的小技巧. 首先要检查默认内容访问帐户是否具有相应的访问权限,或者添加一条相应的爬网规则.如果目标资源库是一个ShareP ...
- Asp.Net实现FORM认证的一些使用技巧(必看篇)
最近因为项目代码重构需要重新整理用户登录和权限控制的部分,现有的代码大体是参照了.NET的FORM认证,并结合了PORTAL KITS的登录控制,代码比较啰嗦,可维护性比较差.于是有了以下的几个需求( ...
- layui时间,table,大图查看,弹出框,获取音频长度,文件上传
1.引入: <link href="../../Scripts/layui-v2.3.0/css/layui.css" rel="stylesheet" ...
- [深入理解Android卷一全文-第四章]深入理解zygote
由于<深入理解Android 卷一>和<深入理解Android卷二>不再出版,而知识的传播不应该由于纸质媒介的问题而中断,所以我将在CSDN博客中全文转发这两本书的所有内容. ...