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

For example: Given binary tree [1,null,2,3],

   1
\
2
/
3

return [1,3,2].

Note: Recursive(递归) solution is trivial, could you do it iteratively(迭代)?

思路:

解法一:用递归方法很简单,

(1)如果root为空,则返回NULL;

(2)如果root->left != NULL,则返回左子树的中序遍历元素;

(3)如果root->right != NULL, 则返回右子树的中序遍历元素;

(4)最后,将左子树的中序遍历元素放入容器,root->val放入容器,再将右子树的中序遍历元素放入容器;

(5)返回容器;

 /**
* 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) {
vector<int> v, v1, v2;
int i;
if(root == NULL)
return v;
if(root->left != NULL)
v1 = inorderTraversal(root->left);
if(root->right != NULL)
v2 = inorderTraversal(root->right);
for(i = ; i < v1.size(); i++)
v.push_back(v1[i]);
v.push_back(root->val);
for(i = ; i < v2.size(); i++)
v.push_back(v2[i]);
return v;
}

解法二:非递归中序遍历二叉树,要定义一个栈(stack)

 class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> v;
stack<TreeNode*> node_stack;
TreeNode* pNode = root;
while((pNode != NULL) || !node_stack.empty()){
//节点不为空,加入栈中,并访问节点左子树
if(pNode != NULL){
node_stack.push(pNode);
pNode = pNode->left;
}
else{
//节点为空,从栈中弹出一个节点,访问这个节点,
pNode = node_stack.top();
node_stack.pop();
v.push_back(pNode->val);
//访问节点右子树
pNode = pNode->right;
}
}
return v;
}
};

Leetcode 94. Binary Tree Inorder Traversal (中序遍历二叉树)的更多相关文章

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

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

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

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

  3. 49. leetcode 94. Binary Tree Inorder Traversal

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

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

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

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

    Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...

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

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

  7. leetcode 94 Binary Tree Inorder Traversal ----- java

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

  8. Leetcode 94 Binary Tree Inorder Traversal 二叉树

    二叉树的中序遍历,即左子树,根, 右子树 /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *lef ...

  9. LeetCode 94. Binary Tree Inorder Traversal 动态演示

    非递归的中序遍历,要用到一个stack class Solution { public: vector<int> inorderTraversal(TreeNode* root) { ve ...

随机推荐

  1. 【转载】strlen与sizeof区别

    自己小结: sizeof使用时,若是数组变量,则是数组变量占的大小 char a[10]; sizeof(a)=10 若是指针,则为指针大小,数组变量作为函数参数传递时,会退化成指针,且函数内是不知道 ...

  2. POJ-2926 Requirements 最远曼哈顿距离

    题目链接:http://poj.org/problem?id=2926 题意:求5维空间的点集中的最远曼哈顿距离.. 降维处理,推荐2009武森<浅谈信息学竞赛中的“0”和“1”>以及&l ...

  3. berserkJS(大名:狂暴JS / 昵称:疯子JS)

    极分享:高质分享+专业互助=没有难做的软件+没有不得已的加班 http://www.finalshares.com/read-6763?f=g-20

  4. 软件开发中的单一职责(转至INFOQ)

    最近在实践微服务化过程中,对其“单一职责”原则深有体会.那么只有微服务化才可以单一职责,才可以解耦吗?答案是否定的. 单一职责原则是这样定义的:单一的功能,并且完全封装起来. 我们做后端Java开发的 ...

  5. elecworks无法连接至协同服务器

    http://jingyan.baidu.com/article/597a0643759e1c312b524385.html 在安装路径中找到Server文件夹,在文件夹中你可以看到只有一个文件[Ew ...

  6. [Objective-c 基础 - 2.5] NSString

    1.NSString基本使用 使用%@占位符输出对象 ; ; NSString *str2 = [NSString stringWithFormat:@"My age is %d and n ...

  7. 转载 ASP.NET中如何取得Request URL的各个部分

    转载原地址 http://blog.miniasp.com/post/2008/02/10/How-Do-I-Get-Paths-and-URL-fragments-from-the-HttpRequ ...

  8. 使用Redis bitmaps进行快速、简单、实时统计

    原文:Fast, easy, realtime metrics using Redis bitmaps (http://blog.getspool.com/2011/11/29/fast-easy-r ...

  9. CSS区块、浮动、定位、溢出、滚动条

    CSS中区块的使用 CSS中浮动的使用 CSS中定位的使用 CSS中溢出的使用 CSS中滚动条的使用 17.1 CSS中区块的使用 属性名称            属性值                ...

  10. cocos2d-x中,简单html富文本显示

    作者:HU 转载请注明,原文链接:http://www.cnblogs.com/xioapingguo/p/4037414.html  虽然自从cocos2d-x更新到3.0后,使用freetype, ...