无论是二叉树的中序遍历还是用 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. 超实用的JavaScript代码段 Item4 --发送短信验证码

    发送短信验证码 实现点击“发送验证码”按钮后,按钮依次显示为“59秒后重试”.“58秒后重试”…直至倒计时至0秒时再恢复显示为“发送验证码”.在倒计时期间按钮为禁用状态 . 第一步.获取按钮.绑定事件 ...

  2. 229. Majority Element II -- 找出数组中出现次数超过 ⌊ n/3 ⌋ 次的数

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

  3. tar.xz如何解压:linux和windows下tar.xz解压命令介绍

    在linux下怎么解压和压缩tar.xz文件? (本文由www.169it.com搜集整理) 在linux下解压tar.xz文件步骤 1 2 # xz -d ***.tar.xz  //先解压xz # ...

  4. 5月23日 JavaScript练习:累加求和

    第一种方法: 第二种方法:

  5. Java集合——Map接口

    1.定义 Map用于保存存在映射关系<key,value>的数据.其中,key值不能重复(使用equals()方法比较),value值可以重复 2.方法 V  put(key,value) ...

  6. kafka客户端代码解析

    转载:http://backend.blog.163.com/blog/static/202294126201431724652597/ 可以使用服务器端下载的kafka二进制包及依赖,也可以通过ma ...

  7. Jquery表单提交方式

    1.使用调用submit方法 function tes1(){ //执行判断 if(校验通过){ $("#formId").submit(); }else{ return; } } ...

  8. mysql把查询的结果格式成日期

    SELECT *,FROM_UNIXTIME(addtime, '%Y-%m-%d %H:%i:%S') as riqi FROM `test`

  9. The APR based Apache Tomcat Native library 异常解决办法

    tomat在linux服务器上启动报The APR based Apache Tomcat Native library which allows optimal performance in pro ...

  10. mysql启动错误

    1.启动时,显示ERROR tail localhost.localdomain.err 错误日志 2.新增目录,启动成功