The recursive solution is trivial and I omit it here.

Iterative Solution using Stack (O(n) time and O(n) space):

 /**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
/**
* @param root: The root of binary tree.
* @return: Inorder in vector which contains node values.
*/
public:
vector<int> inorderTraversal(TreeNode *root) {
// write your code here
vector<int> nodes;
TreeNode* node = root;
stack<TreeNode*> toVisit;
while (node || !toVisit.empty()) {
if (node) {
toVisit.push(node);
node = node -> left;
}
else {
node = toVisit.top();
toVisit.pop();
nodes.push_back(node -> val);
node = node -> right;
}
}
return nodes;
}
};

Another more sophisticated soltuion using Morris Traversal (O(n) time and O(1) space):

 /**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
/**
* @param root: The root of binary tree.
* @return: Inorder in vector which contains node values.
*/
public:
vector<int> inorderTraversal(TreeNode *root) {
// write your code here
vector<int> nodes;
TreeNode* node = root;
while (node) {
if (node -> left) {
TreeNode* predecessor = node -> left;
while (predecessor -> right && predecessor -> right != node)
predecessor = predecessor -> right;
if (!(predecessor -> right)) {
predecessor -> right = node;
node = node -> left;
}
else {
predecessor -> right = NULL;
nodes.push_back(node -> val);
node = node -> right;
}
}
else {
nodes.push_back(node -> val);
node = node -> right;
}
}
return nodes;
}
};

[LintCode] 二叉树的中序遍历的更多相关文章

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

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

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

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

  3. 数据结构《10》----二叉树 Morris 中序遍历

    无论是二叉树的中序遍历还是用 stack 模拟递归, 都需要 O(n)的空间复杂度. Morris 遍历是一种 常数空间 的遍历方法,其本质是 线索二叉树(Threaded Binary Tree), ...

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

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

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

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

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

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

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

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

  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. Git 工作区、暂存区和版本库

    基本概念 我们等来理解下Git工作区.暂存区和版本库概念 工作区:就是你在电脑里能看到的目录 强烈推荐git新手阅读 暂存区:英文叫stage,或index.一般存放在igt 目录下的index文件( ...

  2. Java 继承、多态与类的复用

    摘要: 本文结合Java的类的复用对面向对象两大特征继承和多态进行了全面的介绍. 首先,我们介绍了继承的实质和意义,并探讨了继承,组合和代理在类的复用方面的异同.紧接着,我们依据继承引入了多态.介绍了 ...

  3. jfinal的maven配置

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  4. Java并发编程(九):拓展

    java多线程死锁理解 Java多线程并发最佳实践 Spring与线程安全 HashMap与ConcurrentHashMap 关于java集合类HashMap的理解     ,      数据结构之 ...

  5. Resources.FindObjectsOfTypeAll<T>()的坑(Ghost prefab)

    今天遇到了一个Bug,因为调用Resources.FindObjectsOfTypeAll<T>()遍历整个场景,结果遍历出的对象不对.比较哈希一查果然是两个.原来prefab本身和pre ...

  6. atitit.提升兼容性最佳实践 p825.doc

    atitit.提升兼容性最佳实践 p825.doc 1. Atitit.兼容性的“一加三”策略1 2. 扩展表模式2 3. 同时运行模式2 3.1. 完美的后向兼容性3 3.2. 虚拟机模式3 3.3 ...

  7. ActiveMQ从源代码构建

    众多开源项目.我们一般都是直接拿过来用之而后快. 只是我们也应该知道这些项目是怎样从源代码构建而来的. 既然代码是写出来的,就不能避免有BUG存在,话说没有完美的软件,也没有无漏洞的程序. 事实上从源 ...

  8. redis命令_ZINCRBY

    ZINCRBY key increment member 为有序集 key 的成员 member 的 score 值加上增量 increment . 可以通过传递一个负数值 increment ,让 ...

  9. locationInView和translationInView的区别

      1  translationInView是UIPanGestureRecognizer下面的一个属性 locationInView则是UIGestureRecognizer下面的属性 2  tra ...

  10. layui的单选框

    <script type="text/html" id="radioTpl"> <input type="radio" n ...