LeetCode之“树”:Binary Tree Preorder && Inorder && Postorder Traversal
Binary Tree Preorder Traversal
题目要求:
Given a binary tree, return the preorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3}
,
1
\
2
/
3
return [1,2,3]
.
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> preorderTraversal(TreeNode* root) {
vector<int> rVec;
preorderTraversal(root, rVec);
return rVec;
} void preorderTraversal(TreeNode *tree, vector<int>& rVec)
{
if(!tree)
return; rVec.push_back(tree->val);
preorderTraversal(tree->left, rVec);
preorderTraversal(tree->right, rVec);
}
};
非递归解法
非递归解法相对就要难很多了,理解起来比较抽象。在这里我们需要借助栈。当左子树遍历完后,需要回溯并遍历右子树。具体程序如下:
vector<int> preorderTraversal(TreeNode* root) {
vector<int> rVec;
stack<TreeNode *> st;
TreeNode *tree = root;
while(tree || !st.empty())
{
if(tree)
{
st.push(tree);
rVec.push_back(tree->val);
tree = tree->left;
}
else
{
tree = st.top();
st.pop();
tree = tree->right;
}
} return rVec;
}
更为简洁非递归解法
具体程序如下:
vector<int> preorderTraversal(TreeNode* root) {
vector<int> rVec;
if(!root)
return rVec; stack<TreeNode *> st;
st.push(root);
while(!st.empty())
{
TreeNode *tree = st.top();
rVec.push_back(tree->val);
st.pop();
if(tree->right)
st.push(tree->right);
if(tree->left)
st.push(tree->left);
} return rVec;
}
Binary Tree Inorder Traversal
题目要求:
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?
这道题的非递归解法跟上题的非递归解法基本一致,只是访问的节点时机不一样。详细分析可参考LeetCode上的一篇博文。具体程序如下:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> rVec;
stack<TreeNode *> st;
TreeNode *tree = root;
while(tree || !st.empty())
{
if(tree)
{
st.push(tree);
tree = tree->left;
}
else
{
tree = st.top();
rVec.push_back(tree->val);
st.pop();
tree = tree->right;
}
} return rVec;
}
Binary Tree Postorder Traversal
题目要求:
Given a binary tree, return the postorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3}
,
1
\
2
/
3
return [3,2,1]
.
Note: Recursive solution is trivial, could you do it iteratively?
除了递归法,这道题利用两个栈来解决问题的话会比较方便,详细可参考LeetCode上的一篇文章。具体程序如下:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> rVec;
if(!root)
return rVec; stack<TreeNode *> st;
stack<TreeNode *> output;
st.push(root);
while(!st.empty())
{
TreeNode *tree = st.top();
output.push(tree);
st.pop();
if(tree->left)
st.push(tree->left);
if(tree->right)
st.push(tree->right);
} while(!output.empty())
{
rVec.push_back(output.top()->val);
output.pop();
} return rVec;
}
LeetCode之“树”:Binary Tree Preorder && Inorder && Postorder Traversal的更多相关文章
- LC 144. / 94. / 145. Binary Tree Preorder/ Inorder/ PostOrder Traversal
题目描述 144. Binary Tree Preorder Traversal 94. Binary Tree Inorder Traversal 145. Binary Tree Postorde ...
- [LeetCode] Binary Tree Preorder/Inorder/Postorder Traversal
前中后遍历 递归版 /* Recursive solution */ class Solution { public: vector<int> preorderTraversal(Tree ...
- 【题解】【BT】【Leetcode】Binary Tree Preorder/Inorder/Postorder (Iterative Solution)
[Inorder Traversal] Given a binary tree, return the inorder traversal of its nodes' values. For exam ...
- (二叉树 递归) leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...
- [LeetCode] 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree f
1. Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder travers ...
- Java for LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 31041 Total Submissions: ...
- 【LeetCode】144. Binary Tree Preorder Traversal (3 solutions)
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
随机推荐
- ubuntu日志文件管理
众所周知,ubuntu的日志文件会越来越大,需要定期管理 logrotate是个十分有用的工具,它可以自动对日志进行截断(或轮循).压缩以及删除旧的日志文件.例如,你可以设置logrotate,让/v ...
- 分析RunTime执行命令以及得到返回值
RunTime执行命令得到返回值 我们有在好好几篇博客里提到过RunTime,比如 JAVA之旅(二十三)--System,RunTime,Date,Calendar,Math的数学运算 Androi ...
- EBS开发性能优化之查找需要优化的程序
1.登陆数据库LINUX环境 使用 top 命令查看进程状况 [oratest@ebsdb~]$top top - 15:58:59 up 8 days, 22:04, 1 user, load ...
- Dynamics CRM2013 在Visual Studio中开启脚本的Xrm.Page智能提示
前面篇博文http://blog.csdn.net/vic0228/article/details/49663751提到了通过引用XrmPage-vsdoc.js文件来启用Xrm.Page的智能提示, ...
- 向Github提交代码时遇到的一些问题
今天分享一下我的一些小经验,那就是向github提交我们的代码.之前一直是直接使用的浏览器完成的代码的下载任务,没有使用过客户端,为了让自己在工作之前熟练使用GitHub,所以就有了下面的这篇博文了. ...
- Java基础---Java---面试题---交通灯管理系统(面向对象、枚举)
交通灯管理系统的项目需求: 模拟实现十字路口的交通灯管理系统逻辑,具体需求如下: 1.异步随机生成按照各个路线行驶的车辆 例如: 由南向而来去往北向的车辆-----直行车辆 由西向而来去往南 ...
- 修改android应用包名
由于项目需要,要修改已经开发好的应用包名,这本身很简单,但是如果你没找到门道,可能会白白浪费许多时间. 修改包名有三个地方要改,这三个地方的修改一定要按顺序来,否则你可能会遇到许多不必要的麻烦. 1. ...
- Ruby开发入门
开发环境搭建 首先安装Ruby SDK,我安装的版本是2.0.之后安装IDE,这里用的是Jetbrain的RubyMine 5.4.3,注意是否支持对应版本的Ruby SDK. 一段神奇的注册码... ...
- MySQL聚簇索引的使用介绍
MySQL聚簇索引保证关键字的值相近的元组存储的物理位置也相同(所以字符串类型不宜建立聚簇索引,特别是随机字符串,会使得系统进行大量的移动操作),且一个表只能有一个聚簇索引.因为由存储引擎实现索引,所 ...
- 【一天一道LeetCode】#206. Reverse Linked List
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Reverse ...