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. Linux操作系统--help、man和info工具的区别介绍

    http://wenda.tianya.cn/wenda/thread?tid=1d4b0f172f958833Linux操作系统--help.man和info工具的区别介绍 Linux操作系统为我们 ...

  2. Could not create and/or set value back on to object .

    严重: Error building beanorg.springframework.beans.factory.UnsatisfiedDependencyException: Error creat ...

  3. excel 的几个函数使用

    =IF(表1[[#此行],[state]]="修改",        IF(                ISBLANK(                    VLOOKUP( ...

  4. 电子商务(电销)平台中订单模块(Order)数据库设计明细(转载)

    电子商务(电销)平台中订单模块(Order)数据库设计明细 以下是自己在电子商务系统设计中的订单模块的数据库设计经验总结,而今发表出来一起分享,如有不当,欢迎跟帖讨论~ 订单表 (order)|-- ...

  5. NoSQL(四)

    mongodb介绍 https://www.yiibai.com/mongodb/mongodb_drop_collection.html 1.文档性数据库类似于json对象,分布式 mongodb安 ...

  6. lua工具库penlight--06数据(二)

    词法扫描 虽然 Lua 的字符串模式匹配是非常强大,但需要更强大的东西.pl.lexer.scan可以提供标记字符串,按标记机分类数字.字符串等. > lua -lpl Lua 5.1.4  C ...

  7. 数据库设计(四)数据库的规范化(Normalization)

    数据库的规范化 Database Normalization is a technique of organizing the data in the database. Normalization ...

  8. Xampp + Zend Studio + xDebug 环境搭建 (Mac,Windows都适用)

    这几天折腾了一下PHP开发环境的搭建,现总结一下安装步骤: 1. 安装 Zend Studio,然后破解. 2. 安装 Xampp  3. 配置 Xampp      3.1 配置 Apache服务端 ...

  9. Java设计模式——观察者模式(事件监听)

    最近在看Tomcat和Spring的源码,在启动的时候注册了各种Listener,事件触发的时候就执行,这里就用到了设计模式中的观察者模式. 引-GUI中的事件监听 想想以前在学Java的GUI编程的 ...

  10. UVALive 7712 Confusing Manuscript 字典树 查询与s的编辑距离为1的字符串数量

    /** 题目:UVALive 7712 Confusing Manuscript 链接:https://vjudge.net/problem/UVALive-7712 题意:给定n个不同的字符串,f( ...