今天在切leetcode的时候看到一个Morris算法,用来中序遍历二叉树,非递归,O(1)空间。觉得很强大。记录一下。

基本思想是利用了Threaded Binary Tree

步骤如下:

  1. current节点设置为root。如果current不为空,到2,否则返回;
  2. 如果current没有左子树,输出current的值,current等于current.right;
  3. 如果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 无需使用递归和栈的更多相关文章

  1. 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 ...

  2. 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 ...

  3. [LeetCode] Binary Tree Preorder Traversal 二叉树的先序遍历

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  4. [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

  5. [Algorithm] Construct a Binary Tree and Binary Search

    function createNode(value) { return { value, left: null, right: null }; } function BinaryTree(val) { ...

  6. [LeetCode] 145. Binary Tree Postorder Traversal 二叉树的后序遍历

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

  7. [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 ...

  8. (二叉树 递归) leetcode94. Binary Tree Inorder Traversal

    Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...

  9. (二叉树 递归) 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 ...

随机推荐

  1. (一) Qt Model/View 的简单说明

    (一) Qt Model/View 的简单说明 .预定义模型 (二)使用预定义模型 QstringListModel例子 (三)使用预定义模型QDirModel的例子 (四)Qt实现自定义模型基于QA ...

  2. imx6 uart分析

    本文主要记录: 1.uart设备注册 2.uart驱动注册 3.上层应用调用有些地方理解的还不是很透彻,希望指正. 1.uart设备注册过程 MACHINE_START(MX6Q_SABRESD, & ...

  3. 基于swoole扩展实现真正的PHP数据库连接池

    转自:  http://rango.swoole.com/archives/265 PHP的数据库连接池一直以来都是一个难题,很多从PHP语言转向Java的项目,大多数原因都是因为Java有更好的连接 ...

  4. js中onclick中文参数传输方式

    添加单引号或双引号即可,例: var type = "'"+n.bankCard.type+"'"; var number = "'"+n. ...

  5. 最全Java学习路线图——Java学习指南

    准备篇 适用/适合人群:适合基础小白 目标:掌握JavaSE. ●技术点小节: 1.开发工具的安装配置的介绍 2.JDK安装 3.DOS环境编程 4.Eclipse的安装使用 ●JAVA基础 1.基本 ...

  6. Ubuntu 使用文件 casper-rw 镜像文件 保存变更内容

    yumi工具本可以制作基于u盘的persistent启动盘 casper-rw是ubuntu特有的一种功能,支持liveCD启动的ubuntu系统保存用户的变更内容 那我们想同iso光盘从硬盘上启动, ...

  7. 关于截取字符串substr和substring两者的区别

    https://blog.csdn.net/adley_function/article/details/52130762 substr和substring两个都是截取字符串的. 两者有相同点,如果只 ...

  8. linq在获取部门层级树种的应用

    public string GetNavigationsJson() { AjaxA_NAVIGATIONS ajaxnavigations = new AjaxA_NAVIGATIONS(); IL ...

  9. 面向对象----构造方法、this 关键字、函数的参数传递、package语句/import语句

    构造方法 构造器的定义.作用 构造方法的特征 它具有与类相同的名称:它不含返回值: 注意:在构造方法里不含返回值的概念是不同于“void”的,在定义构造方法时加了“void”,结果这个方法就不再被自动 ...

  10. js里面函数的内部属性

    1.arguments用來存放传输参数的集合,可以被调用多次,每次数組都不一样,增强了函数的强壮性 实例: function calc() { var sum = 0; /*参数为一个时候*/ if ...