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. NOIP2014 寻找道路

    2.寻找道路 (road.cpp/c/pas) [问题描述] 在有向图G中,每条边的长度均为1,现给定起点和终点,请你在图中找一条从起点到终点的路径,该路径满足以下条件: 1.路径上的所有点的出边所指 ...

  2. 在C中定义一个动态的二维数组

    一般来讲两种办法: 第一种:连续内存分配 #include "stdio.h" #include "stdlib.h" int main() { int x,y ...

  3. 关于ANSI 和 Unicode

    关于ANSI和Unicode 1.ANSI American National Standards Institute(美国国家标准学会),ANSI编码不是一种具体的编码方式,而是一种指定在某些环境下 ...

  4. <转>HTML中的table转为excel

    转换html 中的table 为excel,firefox浏览器支持,代码如下 <%@ page language="java" contentType="text ...

  5. tomcat 7 用户设置

    在tomcat/conf/tomcat-users.xml加入如下脚本就可以了 <role rolename="admin-gui"/> <role rolena ...

  6. 问题-WIN7 ..\Bin\InitCC32.exe".进程无法访问(拒绝访问)

    问题现象: 问题原因:是InitCC32.exe没有权限. 问题处理:在DELPHI7的安装目录里设置用户权限,加入EVE... 这个用户.

  7. Resharp非常实用的快捷键

    Alt+Home  定位到父类.父接口 Alt + End 定位到子类 Ctrl+T     快速在整个解决方案下搜索 类型.方法.文件夹 Alt+Ctrl+Spance  给出提示框 Shift+F ...

  8. Java WeakReference的理解与使用

    转载:http://itindex.net/detail/47754-%E9%9D%A2%E8%AF%95-java-weakreference?utm_source=tuicool&utm_ ...

  9. jquery 延迟加载代码

    <!--引入以下两个js文件--> <script type="text/javascript" src="./js/jquery.min.js&quo ...

  10. 推荐eclipse velocity一款插件 --- veloeclipse

    vm文件在eclipse展示很丑,关键字没有颜色之差.这里,推荐一款极其好用的velocity插件  -- veloeclipse 在 Eclipse 版本 4.5.0, 离线安装 Veloeclip ...