无论是二叉树的中序遍历还是用 stack 模拟递归, 都需要 O(n)的空间复杂度。

Morris 遍历是一种 常数空间 的遍历方法,其本质是 线索二叉树(Threaded Binary Tree), 本质上其实就是利用二叉树中 n+1 个指向NULL的指针。

关于 线索二叉树 见 http://blog.csdn.net/shoulinjun/article/details/19037819

Morris 遍历在遍历的过程中,通过利用叶子节点空的right指针,指向中序遍历的后继节点,从而避免了对 stack 的依赖。

// Morris Traversal
// space effienct
void InOrderVisitMorris(TreeNode *root)
{
TreeNode *pre(NULL);
while(root)
{
// 左子树是空,直接访问该节点,然后访问右子树
if(root->left_child == NULL){
cout << root->value << " ";
root = root->right_child;
continue;
}
// 找 root 的前驱节点
for(pre = root->left_child; pre->right_child && pre->right_child != root; pre = pre->right_child); // 第一次访问root, 建立线索
if(pre->right_child == NULL){
pre->right_child = root;
root = root->left_child;
}
// 第二次访问,说明左子树已经访问过
else{
pre->right_child = NULL;
cout << root->value << " ";
root = root->right_child;
}
}
}

数据结构《10》----二叉树 Morris 中序遍历的更多相关文章

  1. [LeetCode] Binary Tree Inorder Traversal 二叉树的中序遍历

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

  2. LeetCode(94):二叉树的中序遍历

    Medium! 题目描述: 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗 ...

  3. LeetCode 94:二叉树的中序遍历 Binary Tree Inorder Traversal

    题目: 给定一个二叉树,返回它的中序 遍历. Given a binary tree, return the inorder traversal of its nodes' values. 示例: 输 ...

  4. 【LeetCode】94. 二叉树的中序遍历

    94. 二叉树的中序遍历 知识点:二叉树:递归:Morris遍历 题目描述 给定一个二叉树的根节点 root ,返回它的 中序 遍历. 示例 输入:root = [1,null,2,3] 输出:[1, ...

  5. lintcode:二叉树的中序遍历

    题目: 二叉树的中序遍历 给出一棵二叉树,返回其中序遍历 样例 给出二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [1,3,2]. 挑战 你能使用非递归算法来实现么? 解题: 程序直接来源 ...

  6. 【LeetCode题解】94_二叉树的中序遍历

    目录 [LeetCode题解]94_二叉树的中序遍历 描述 方法一:递归 Java 代码 Python代码 方法二:非递归 Java 代码 Python 代码 [LeetCode题解]94_二叉树的中 ...

  7. LintCode-67.二叉树的中序遍历

    二叉树的中序遍历 给出一棵二叉树,返回其中序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 返回 [1,3,2]. 挑战 你能使用非递归实现么? 标签 递归 二叉树 二叉树遍历 code /** ...

  8. LeetCode 94. 二叉树的中序遍历(Binary Tree Inorder Traversal)

    94. 二叉树的中序遍历 94. Binary Tree Inorder Traversal 题目描述 给定一个二叉树,返回它的 中序 遍历. LeetCode94. Binary Tree Inor ...

  9. Leetcode题目94.二叉树的中序遍历(中等)

    题目描述: 给定一个二叉树,返回它的中序遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 思路解析: 1 ...

随机推荐

  1. hdu------(1525)Euclid's Game(博弈决策树)

    Euclid's Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  2. 点击每一个button,弹出相应的索引号

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. 【源码下载】分享一个支持自安装自卸载的Windows服务

    NetworkComms网络通信框架序言 这个程序来自  www.codeproject.com 具体的出处就忘了 服务器端的程序,我一般采用在windows服务中调用打开的方式,这样既能看到界面,又 ...

  4. MySQL存储过程之事务管理

    原文链接:http://hideto.iteye.com/blog/195275 MySQL存储过程之事务管理 ACID:Atomic.Consistent.Isolated.Durable 存储程序 ...

  5. js 替换 当前URL 特定参数

    js 替换 当前URL 特定参数 2012-12-24 20:45:53|  分类: JS&JQuery |举报 |字号 订阅   //替换指定传入参数的值,paramName为参数,repl ...

  6. js基础之事件

    一.event对象 document.onclick=function(ev){ oEvent=event?event:ev;//兼容性写法 alert(oEvent.clientX); alert( ...

  7. PowerMock使用遇到的一些问题

    首先使用PowerMock  Mock对象如果不成功的话首先要检查在测试类上是否有这两个声明@RunWith(PowerMockRunner.class)                        ...

  8. Sticks(poj1011/uva307)

    题目大意: 乔治有一些碎木棒,是通过将一些相等长度的原始木棒折断得到的,给出碎木棒的总数和各自的长度,求最小的可能的原始木棒的长度:(就是将一些正整数分组,每组加起来和相等,使和尽可能小) 一开始做p ...

  9. Terminating app due to uncaught

    1>Images.xcassets: A 60x60@2x app icon is required for iPhone apps targeting iOS 7.0 and later 图片 ...

  10. POJ 1700 F(Contest #3)

    Description A group of N people wishes to go across a river with only one boat, which can at most ca ...