Leetcode 94. Binary Tree Inorder Traversal (中序遍历二叉树)
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 (中序遍历二叉树)的更多相关文章
- 094 Binary Tree Inorder Traversal 中序遍历二叉树
给定一个二叉树,返回其中序遍历.例如:给定二叉树 [1,null,2,3], 1 \ 2 / 3返回 [1,3,2].说明: 递归算法很简单,你可以通过迭代算法完成吗?详见 ...
- [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历
题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...
- 49. leetcode 94. Binary Tree Inorder Traversal
94. Binary Tree Inorder Traversal 二叉树的中序遍历 递归方法: 非递归:要借助栈,可以利用C++的stack
- LeetCode 94. Binary Tree Inorder Traversal 二叉树的中序遍历 C++
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [,,] \ / Out ...
- [leetcode]94. Binary Tree Inorder Traversal二叉树中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...
- leetCode 94.Binary Tree Inorder Traversal(二叉树中序遍历) 解题思路和方法
Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...
- leetcode 94 Binary Tree Inorder Traversal ----- java
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- Leetcode 94 Binary Tree Inorder Traversal 二叉树
二叉树的中序遍历,即左子树,根, 右子树 /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *lef ...
- LeetCode 94. Binary Tree Inorder Traversal 动态演示
非递归的中序遍历,要用到一个stack class Solution { public: vector<int> inorderTraversal(TreeNode* root) { ve ...
随机推荐
- SimpleHttpServer的学习(1)
闲来没事,分析一下一个简单的HttpServer github地址: https://github.com/Filirom1/SimpleHTTPServer 实现的功能很简单就是一个FTP服务器 默 ...
- POJ 2986 A Triangle and a Circle 圆与三角形的公共面积
计算几何模板 #include<stdio.h> #include<string.h> #include<stdlib.h> #include<math.h& ...
- JSFのAjaxタグのoneventでbegin/complete/successを使う
PrimeFacesに慣れてしまって.通常のHTMLタグでの記述方法がわからなかったりする点があった…ので.メモ. Ajaxでリクエスト送信のタイミングやレスポンスが戻るタイミングに何らか(JavaS ...
- Struts2 url传递中文出现乱码
项目所有的编码都改为了utf-8.在tomcat的 server.xml中修改下面这段 <Connector port="8080" protocol="HTTP/ ...
- ZOJ-3593 One Person Game 概率DP
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3593 带环的概率DP一般的做法是求出转移方程,然后高斯消元解方程 ...
- light oj 1138
Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submit Status Pract ...
- [OC Foundation框架 - 23] 文件管理
A. 目录管理 NSFileManager*manager = [NSFileManagerdefaultManager];//单例模式 // 1.获取文件属性 NSString *path = @& ...
- jquery-ui 之draggable详解
举一个例子: <div class="box"> <div id="draggable"> <p>Drag me aroun ...
- Visual Studio 2012 应用软件开发新方式
微软正式发布Visual Studio 2012 应用软件开发新方式 2012-09-13 09:54 51CTO.com 我要评论(0) 字号:T | T “现在,开发者将有更好的机会开发与云服务连 ...
- Oracle中遍历Ref Cursor示例
示例编写环境 数据库:Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 - 64bit Production 登陆用户:Scott O ...