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. pandas(零)数据结构

    pandas的两个主要的数据结构: Series series是一种类似于一维数组的对象,它由一组数据(NumPy数组类型的数据)和一组与之相关的数据标签(索引)组成. from pandas imp ...

  2. IT basic knowledge

    LAMP: Linux Apache MySQL PHP URL & URI: 统一资源定位符是统一资源标志符的一个下种.统一资源标志符确定一个资源,而统一资源定位符不但确定一个资源, 而且还 ...

  3. 02 Spring框架 简单配置和三种bean的创建方式

    整理了一下之前学习Spring框架时候的一点笔记.如有错误欢迎指正,不喜勿喷. 上一节学习了如何搭建SpringIOC的环境,下一步我们就来讨论一下如何利用ioc来管理对象和维护对象关系. <? ...

  4. str字符串、bool类型常用方法总结

    字符串拼接 必须是字符串与字符串拼接 print('马化腾'+'马云') print('马化腾' * 10) 将打印10个马化腾 字符串翻转 [ : :-1] 字符串可以加和乘,不能减和乘 input ...

  5. linux免密登录配置

    第一步:安装openssh-clients yum install -y openssh-clients.x86_64第二步:生成密钥 ssh-keygen第三步:拷贝公钥到其他机器 ssh-copy ...

  6. Delphi 2005 以上版本GIF动画播放设置

    源: Delphi 2005 以上版本GIF动画播放设置

  7. Java中的HashMap

    今天到中关村软件园面试被问到Java中HashMap的存值原理,瞬间无言已对,回答用了一个数组,然后沉默,面试官说,一次的面试失败不算什么,之后...... 1.关于hashCode hashCode ...

  8. Cisco学习笔记

    目录 1. 路由 1.1 静态路由 1.2 动态路由 2. 访问控制列表 2.1 标准访问控制列表 2.2 扩展访问控制列表 2.3 命名访问控制列表 3. VLAN 3.1 基础知识 3.2 配置实 ...

  9. 【BZOJ2818】Gcd (欧拉函数)

    网址:http://www.lydsy.com/JudgeOnline/problem.php?id=2818 一道数论裸题,欧拉函数前缀和搞一下就行了. 小于n的gcd为p的无序数对,就是phi(1 ...

  10. How to use Jenkins

    一.关键点 1.how to start the build server? do i need to start some app to do this? I don't believe so... ...