Given a binary tree, return the inorder traversal of its nodes' values.

For example:
Given binary tree {1,#,2,3},

   1
\
2
/
3

return [1,3,2].

Note: Recursive solution is trivial, could you do it iteratively?
中序遍历二叉树,递归遍历当然很容易,题目还要求不用递归,下面给出两种方法:

递归:

 /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
if(!root) return ret;
tranverse(root);
return ret;
} void tranverse(TreeNode * root)
{
if(!root) return;
tranverse(root->left);
ret.push_back(root->val);
tranverse(root->right);
}
private:
vector<int> ret;
};

迭代:

 class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> ret;
if(!root) return ret;
map<TreeNode *, bool> m;
stack<TreeNode *> s;
s.push(root);
while(!s.empty()){
TreeNode * t = s.top();
if(t->left && !m[t->left]){
m[t->left] = true;
s.push(t->left);
t = t->left;
continue;
}
ret.push_back(t->val);
s.pop();
if(t->right && !m[t->right]){
m[t->right] = true;
s.push(t->right);
t = t->right;
}
}
return ret;
}
};

java版本的代码如下所示,首先是递归版本的代码:

 public class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<Integer>();
recur(list, root);
return list;
} public void recur(List<Integer> list, TreeNode root){
if(root == null)
return;
if(root.left != null)
recur(list, root.left);
list.add(root.val);
if(root.right != null)
recur(list, root.right);
}
}

再是非递归:

 public class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
Stack<TreeNode> s = new Stack<TreeNode>();
List<Integer> l = new ArrayList<Integer>();
Map<TreeNode, Integer> m = new HashMap<TreeNode, Integer>();
if(root != null)
s.push(root);
while(!s.empty()){
TreeNode t = s.peek();
while(t.left != null && !m.containsKey(t.left)){
s.push(t.left);
m.put(t.left, );
t = t.left;
}
s.pop();
l.add(t.val);
if(t.right != null && !m.containsKey(t.right)){
s.push(t.right);
m.put(t.right, );
}
}
return l;
}
}

LeetCode OJ:Binary Tree Inorder Traversal(中序遍历二叉树)的更多相关文章

  1. Leetcode 94. Binary Tree Inorder Traversal (中序遍历二叉树)

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

  2. 094 Binary Tree Inorder Traversal 中序遍历二叉树

    给定一个二叉树,返回其中序遍历.例如:给定二叉树 [1,null,2,3],   1    \     2    /   3返回 [1,3,2].说明: 递归算法很简单,你可以通过迭代算法完成吗?详见 ...

  3. LeetCode OJ——Binary Tree Inorder Traversal

    http://oj.leetcode.com/problems/binary-tree-inorder-traversal/ 树的中序遍历,递归方法,和非递归方法. /** * Definition ...

  4. [LeetCode] Binary Tree Inorder Traversal 中序排序

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

  5. [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历

    题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...

  6. 49. leetcode 94. Binary Tree Inorder Traversal

    94. Binary Tree Inorder Traversal    二叉树的中序遍历 递归方法: 非递归:要借助栈,可以利用C++的stack

  7. leetcode -day29 Binary Tree Inorder Traversal &amp; Restore IP Addresses

    1.  Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' ...

  8. 【LeetCode】Binary Tree Inorder Traversal

    Binary Tree Inorder Traversal Total Accepted: 16406 Total Submissions: 47212My Submissions Given a b ...

  9. leetCode 94.Binary Tree Inorder Traversal(二叉树中序遍历) 解题思路和方法

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

  10. LeetCode 94. Binary Tree Inorder Traversal 二叉树的中序遍历 C++

    Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [,,] \ / Out ...

随机推荐

  1. swift笔记——环境搭建及Hello,Swift!

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/enson16855/article/details/29829601 首先要下载XCode6,仅仅有 ...

  2. Android学习资源网站大全

    https://github.com/zhujun2730/Android-Learning-Resources 整理了一些 Android 的博客链接.学习资源网站.站在巨人的肩膀上,会看得更远.整 ...

  3. Spark架构解析(转)

    Application: Application是创建了SparkContext实例对象的Spark用户,包含了Driver程序, Spark-shell是一个应用程序,因为spark-shell在启 ...

  4. HAProxy配置参数说明

    一.全局配置"global"配置中的参数为进程级别的参数,且通常与其运行的OS相关.1.进程管理及安全相关的参数chroot <jail dir>修改haproxy的工 ...

  5. pandas(一)操作Series和DataFrame的基本功能

    reindex:重新索引 pandas对象有一个重要的方法reindex,作用:创建一个适应新索引的新对象 以Series为例 >>> series_obj = Series([4. ...

  6. Amend Last Commit选项使用注意点

    1.创建master分支并上传到remote分支 create 2 folders and 4 files 2.修改folder1/file1.java,commit并push. 3.修改folder ...

  7. Web Deploy 安装及问题解决

    注意: 站点名称:  服务器上IIS的站点名称.  . 我之前这里随便写一直不成功. 返回500..... 用户名, 密码:  这里最好用windows帐号. 问题比较少. 目标URL: 可不写. 可 ...

  8. Spring:笔记整理(2)——IOC容器

    IOC容器 什么是IOC 说明 IOC ,全称Inversion of control,即,控制反转,是一种设计思想. 控制: 在Java中,IOC意味着:你将设计好的对象交给容器控制,而不是传统的在 ...

  9. ajax json 异步请求

    function ajaxTest(){ if (true) { $.ajaxSettings.async = false; var dataJson; $.getJSON("/univer ...

  10. ETL应用:使用shell实现文件级校验的方法

    BI应用中,对接口规范性约束很重要,接口文件提供需要配套提供该文件的校验文件,校验文件格式如下: 序号 信息内容 数据类型及长度 说明 1 接口数据文件名称 CHAR(50) 2 文件的大小(字节数) ...