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 ...
随机推荐
- PGM:贝叶斯网的参数估计
http://blog.csdn.net/pipisorry/article/details/52578631 本文讨论(完备数据的)贝叶斯网的参数估计问题:贝叶斯网的MLE最大似然估计和贝叶斯估计. ...
- 用类模拟C风格的赋值+返回值
这个方法比较好: class DataHolder: def __init__(self, value=None): self.value = value def set(self, value): ...
- 使用MD5SUM检查文件
有不少网站提供下载文件的同时,提供了文件的MD5SUM的值.如何检查自己下载的文件与原文件一样呢?用md5sum的-c选项. 操作如下: 1.先新建一个文本文件,写入网站上提供的md5sum的值,空两 ...
- C语言诠释--为什么内存是线性分布的。
Author:伟易达集团软件工程师 II 杨源鑫Date :2016.11.11Subject:内存为什么是线性分布的 今天有位小伙伴问了我一个问题,问题大概是这样描述的: 师兄,我如何能够 ...
- Java多线程模型
谈到Java多线程就涉及到多线程的模型及Java线程与底层操作系统之间的关系.正如我们熟知,现代机器可以分为硬件和软件两大块,如图2-5-1-1,硬件是基础,软件提供实现不同功能的手段.而且软件可以分 ...
- android 集成微博常见问题
我们在做微博集成登录.分享.聊天的时候,肯定会遇到很多的坑,这里总结下常见的问题. 文件不存在 C8998 的解决方法 如图我们走微博授权登录的时候如果OAuth2.0 授权设置回调页面设置和本地的不 ...
- windows系统下安装和使用ROS的解决方案 (1 win_ros 2 rosserial_windows)
具体请参考官网: 1 http://wiki.ros.org/win_ros 2 https://github.com/ros-windows/win_ros 3 http://wiki.ros ...
- adb -s 设备名 设备名还有非法字符
当有多台安卓设备在同一电脑上时 想敲adb控制某一个设备 需要如下格式 adb -s 设备名 设备名 可以用adb devices获取 当发现adb devices 获取的名字是特别长而且含有非法字符 ...
- 4.4、Android Studio在命令行运行Gradle
默认情况下,在你的Gradle构建设置中有两种构建类型:一种是为了调试你的应用,debug类型:一种是构建最终的发布版本,release类型.无论你使用哪种模式,你的app必须在安装到设备或虚拟机中之 ...
- Java在Linux下 不能处理图形的解决办法 Can't connect to X11 window server
java在图形处理时调用了本地的图形处理库.在利用Java作图形处理(比如:图片缩放,图片签名,生成报表)时,如果运行在windows上不会出问题.如果将程序移植到Linux/Unix上的时候有可能出 ...