LC 144. / 94. / 145. Binary Tree Preorder/ Inorder/ PostOrder Traversal
题目描述
前序排列 :根-左-右
中序排列: 左-根-右
后序排列:左-右-根
参考答案
// PreOrder
/**
* 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 {
private:
vector<int> res; public:
vector<int> preorderTraversal(TreeNode* root) {
foo(root);
return res;
}
void foo(TreeNode* root){
if(root == NULL)
return; res.push_back(root->val);
foo(root->left);
foo(root->right);
}
}; // InOrder
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
if(root == NULL){
return vector<int>();
}
vector<int> result; // 创建结果
stack<TreeNode *> s;// 创建堆栈
TreeNode *p = root; // 临时让p为root
// 寻找p最左边的node
while(p!=NULL){
s.push(p); // 从root开始,将左边的node推入stack
p = p->left;// 更新p为左node
}
// s 为全是左节点的stack
// 对 s进行循环操作
while(!s.empty()){
// 将最最左边的,推入stack
p = s.top();
result.push_back(p->val);
s.pop(); // 自己消失了
// 然后观察这个的右边node
if(p->right != NULL){
p = p->right;
while(p!=NULL){ //观察node的左边
s.push(p);
p = p->left;
}
}
}
return result;
}
}; // PostOrder class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
if(root == NULL){
return vector<int>();
}
vector<int> result;
stack<TreeNode *> s; s.push(root);
while(!s.empty()){
TreeNode *temp = s.top();
result.push_back(temp->val);
s.pop();
if(temp->left!=NULL){
s.push(temp->left);
}
if(temp->right!= NULL){
s.push(temp->right);
}
}
reverse(result.begin(), result.end());
return result; }
};
LC 144. / 94. / 145. Binary Tree Preorder/ Inorder/ PostOrder Traversal的更多相关文章
- LeetCode之“树”:Binary Tree Preorder && Inorder && Postorder Traversal
Binary Tree Preorder Traversal 题目链接 题目要求: Given a binary tree, return the preorder traversal of its ...
- [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 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)
翻译 给定一个二叉树.返回其兴许遍历的节点的值. 比如: 给定二叉树为 {1. #, 2, 3} 1 \ 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 ...
- [LeetCode] 145. Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- 145. Binary Tree Postorder Traversal
题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given bina ...
- (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...
- 12. Binary Tree Postorder Traversal && Binary Tree Preorder Traversal
详见:剑指 Offer 题目汇总索引:第6题 Binary Tree Postorder Traversal Given a binary tree, return the po ...
- Binary Tree Preorder Traversal and Binary Tree Postorder Traversal
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
随机推荐
- Cannot initialize a variable of type 'Stu *' with an rvalue of type 'void *'
code: 将 Stu* pStu = malloc(sizeof(Stu)); 改为Stu* pStu = (Stu*)malloc(sizeof(Stu)); code #include < ...
- luogu P4168 蒲公英+ 分块学习笔记
传送门 题目描述 在乡下的小路旁种着许多蒲公英,而我们的问题正是与这些蒲公英有关. 为了简化起见,我们把所有的蒲公英看成一个长度为n的序列\((a_1,a_2..a_n)\),其中 \(a_i\)为一 ...
- 内存分析工具 MAT 的使用【转】
转自:http://blog.csdn.net/aaa2832/article/details/19419679/ 1 内存泄漏的排查方法 Dalvik Debug Monitor Server (D ...
- [SDOI2019]快速查询——模拟
题目链接: [SDOI2019]快速查询 对于整个序列维护一个标记$(k,b)$表示序列的每个数的真实值为$k*a_{i}+b$(注意要实时维护$k$的逆元),并记录序列的和. 对于单点修改,将$a_ ...
- Telegraf+Influxdb+Grafana(Windows下本机简易监控系统搭建)
1.文件名称 telegraf-1.5.0_windows_amd64.zip influxdb-1.4.2_windows_amd64.zip grafana-4.6.3.windows-x64.z ...
- centos7 windows7 双系统重新构建引导和启动顺序
安装centos后无法引导启动windows7的解决方法 在电脑Windows7系统上安装Centos7,安装后找不到Windows7引导菜单. 原因:因为CentOS 7已采用新式的grub2系统, ...
- 实现一个微信小程序组件:文字跑马灯效果
marquee.json { "component": true, "usingComponents": {} } marquee.wxml <!--co ...
- c++ 套接字 --->2002 java NIO --->netty
c++ 套接字 --->2002 java NIO --->netty
- Apache RocketMQ 的过去、现在和未来 原创: DataPipeline DataPipeline数见科技 前天
Apache RocketMQ 的过去.现在和未来 原创: DataPipeline DataPipeline数见科技 前天
- Android 定义和使用样式
如图,在stryle.xml中定义样式 然后可以在布局文件中使用样式