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 ...
随机推荐
- Bzoj 4591: [Shoi2015]超能粒子炮·改 数论,Lucas定理,排列组合
4591: [Shoi2015]超能粒子炮·改 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 178 Solved: 70[Submit][Stat ...
- PHP算法之二分查找和顺序查找
一.二分查找 (数组里查找某个元素) /** * 二分查找 (数组里查找某个元素) * $k为要查找的关键字(注:待查找的数组元素为奇数个)$low为查找范围的最小键值,$high为查找范围的最大键值 ...
- 【转载】linux命令行计算器bc的一个“坑”
[转载自]http://blog.chinaunix.net/uid-174325-id-3518953.html 结论:ibase,obase可以使用在不同的计算公式里,但是尽量把obase放iba ...
- 恒天云IaaS基础设施标准
系统总体要求: 支持多种操作系统:支持Windows,Redhat.Suse等Linux操作系统: 支持多种虚拟化系统:支持多种计算资源虚拟化方式: 网络接口:支持千兆及万兆以太网技术: 供电:支持直 ...
- HDU-4639 Hehe 简单DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4639 简单递推题,呵呵,不多说... //STATUS:C++_AC_15MS_272KB #incl ...
- berserkJS(大名:狂暴JS / 昵称:疯子JS)
极分享:高质分享+专业互助=没有难做的软件+没有不得已的加班 http://www.finalshares.com/read-6763?f=g-20
- Codeforces Round #105 (Div. 2) ABCDE
A. Insomnia cure 哎 只能说英语太差,一眼题我看了三分钟. 题意:给5个数k, l, m, n 和 d,求1~d中能被k, l, m, n 至少一个整除的数的个数. 题解:…… 代码: ...
- Android的事件处理
1 android事件处理概述 不论是桌面应用还是手机应用程序,面对最多的就是用户,经常需要处理用户的动作-------也就是需要为用户动作提供响应,这种为用户动作提供响应的机制就是事件处理.andr ...
- Unity3D之AssetBundle学习:Android上运行笔记
路径统一 在Android上加载StreamingAssets文件夹下的AssetBundle文件,首先需要对加载地址进行处理,注意PC.Android和IOS的地址不一致需要针对不同的平台不同的处理 ...
- 【Away3D代码解读】(一):主要类及说明
在深入解读Away3D的代码之前,需要对其有个大概的认识.本节主要列出Away3D中常用的类,并附上说明: View3D: Away3D的入口类,即创建该类就会初始化一个可以使用GPU呈现3D的对象, ...