The recursive solution is trivial and I omit it here.

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

  1. /**
  2. * Definition of TreeNode:
  3. * class TreeNode {
  4. * public:
  5. * int val;
  6. * TreeNode *left, *right;
  7. * TreeNode(int val) {
  8. * this->val = val;
  9. * this->left = this->right = NULL;
  10. * }
  11. * }
  12. */
  13. class Solution {
  14. /**
  15. * @param root: The root of binary tree.
  16. * @return: Inorder in vector which contains node values.
  17. */
  18. public:
  19. vector<int> inorderTraversal(TreeNode *root) {
  20. // write your code here
  21. vector<int> nodes;
  22. TreeNode* node = root;
  23. stack<TreeNode*> toVisit;
  24. while (node || !toVisit.empty()) {
  25. if (node) {
  26. toVisit.push(node);
  27. node = node -> left;
  28. }
  29. else {
  30. node = toVisit.top();
  31. toVisit.pop();
  32. nodes.push_back(node -> val);
  33. node = node -> right;
  34. }
  35. }
  36. return nodes;
  37. }
  38. };

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

  1. /**
  2. * Definition of TreeNode:
  3. * class TreeNode {
  4. * public:
  5. * int val;
  6. * TreeNode *left, *right;
  7. * TreeNode(int val) {
  8. * this->val = val;
  9. * this->left = this->right = NULL;
  10. * }
  11. * }
  12. */
  13. class Solution {
  14. /**
  15. * @param root: The root of binary tree.
  16. * @return: Inorder in vector which contains node values.
  17. */
  18. public:
  19. vector<int> inorderTraversal(TreeNode *root) {
  20. // write your code here
  21. vector<int> nodes;
  22. TreeNode* node = root;
  23. while (node) {
  24. if (node -> left) {
  25. TreeNode* predecessor = node -> left;
  26. while (predecessor -> right && predecessor -> right != node)
  27. predecessor = predecessor -> right;
  28. if (!(predecessor -> right)) {
  29. predecessor -> right = node;
  30. node = node -> left;
  31. }
  32. else {
  33. predecessor -> right = NULL;
  34. nodes.push_back(node -> val);
  35. node = node -> right;
  36. }
  37. }
  38. else {
  39. nodes.push_back(node -> val);
  40. node = node -> right;
  41. }
  42. }
  43. return nodes;
  44. }
  45. };

[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. SSH登陆响应慢的问题

    http://xiaobin.net/201112/ssh-login-quite-slow/ 同样的问题,有可能是两种情况: 第一种情况比较常见,也有很多资料提及到,就是在SSH登陆时服务器端会对客 ...

  2. Linux 压缩文件的命令行总结

    Linux压缩文件的读取 ·    *.Z       compress 程序压缩的档案: ·    *.bz2     bzip2 程序压缩的档案: ·    *.gz      gzip 程序压缩 ...

  3. SQL SERVER 如何处理带字母的自增列--【叶子】

    --需求说明: /* id         col ---------- ---------- AB00001    a AB00002    b --当再插入数据的时候让id自动变成AB00003 ...

  4. 部署NopCommerce商城系统问题整理

    NopCommerce是一个很棒的开源商城系统,下面整理一下我在部署使用NopCommerce系统中的一些问题. 我使用的是NopCommerce3.9版本. 1.安装 安装教程网上很多,这里不细说, ...

  5. android http post 请求与 json字符串

    一.目标 android客户端发送一个json格式的http的请求,期望得到服务端的一个json反馈. 1. 客户端发送的json格式为: {"data" : "valu ...

  6. JUC组件扩展(二)-JAVA并行框架Fork/Join(一):简介和代码示例

    一.背景 虽然目前处理器核心数已经发展到很大数目,但是按任务并发处理并不能完全充分的利用处理器资源,因为一般的应用程序没有那么多的并发处理任务.基于这种现状,考虑把一个任务拆分成多个单元,每个单元分别 ...

  7. 未设置BufferSize导致FTP下载速度过慢的问题

    開始下载前设置BufferSize就可以解决: ftpClient.setBufferSize(1024*1024); 查看commons-net的源代码.能够发现假设未设置该參数.将会一个字节一个字 ...

  8. Java Servlet/JSP容器配置 session id

    http://www.eclipse.org/jetty/documentation/current/session-management.html#setting-session-character ...

  9. 【flink training】 打车热点区域实时统计PopularPlaces

    http://training.data-artisans.com/是Apache Flink商业公司DataArtisans提供的一个flink学习平台,主要提供了一些业务场景和flink api结 ...

  10. PHP进制转换[实现2、8、16、36、64进制至10进制相互转换]

    自己写了一个PHP进制转换程序,一个类吧,第一次写这个东东,写这个东东,在处理文本文件时能用得到.   可以实现: 10进制转换2.8.16.36.62进制2.8.16.36.62进制转换10进制 有 ...