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 ...
随机推荐
- Java Heap dump文件分析工具jhat简介
jhat 是Java堆分析工具(Java heap Analyzes Tool). 在JDK6u7之后成为标配. 使用该命令需要有一定的Java开发经验,官方不对此工具提供技术支持和客户服务. 用法: ...
- 如何设置fvOptions【翻译】
翻译自:CFD-online 帖子地址:http://www.cfd-online.com/Forums/openfoam-pre-processing/121763-how-set-fvoption ...
- 缓冲区 cin() getline() getchar()
; } 只输入了一行abc, 然后回车后就输出一行abc, 一行空 这是因为cin在缓冲区里丢下的\n 给后面的getline()当成输入结束的标志了 二.getline(cin, str) 输入结束 ...
- Kettle中ETL的效率优化
ETL效率优化 开启数据库日志记录及性能监控 如果我们想要优化一个ETL(KTR或者KJB)的性能,我们首先需要知道的就是它的瓶颈在哪里.而这些信息一般只能在ETL运行的步骤度量中看到,并且是不会持久 ...
- VUE -- iview table 组件 中使用 upload组件 上传组件 on render 事件不会触发问题
碰到的问题是: upload 组件在 on中写的监听事件不会被触发 在 props 中来监听:==>
- 【分类算法】K近邻(KNN) ——kd树(转载)
K近邻(KNN)的核心算法是kd树,转载如下几个链接: [量化课堂]一只兔子帮你理解 kNN [量化课堂]kd 树算法之思路篇 [量化课堂]kd 树算法之详细篇
- VS2015编译gdal库 debug
nmake -f makefile.vc DEBUG=1nmake /f makefile.vc installnmake /f makefile.vc devinstal E:\Visual Stu ...
- 算法习题---4.3救济金发放(UVa133)
一:题目 (n< )个人站成一圈,逆时针编号为1~n.有两个官员,A从1开始逆时针数,B从n开始顺时针数.在每一轮中,官员A数k个就停下来,官员B数m个就停下来(注意有可能两个官员停在同一个人上 ...
- PAT 甲级 1059 Prime Factors (25 分) ((新学)快速质因数分解,注意1=1)
1059 Prime Factors (25 分) Given any positive integer N, you are supposed to find all of its prime ...
- PAT 甲级 1051 Pop Sequence (25 分)(模拟栈,较简单)
1051 Pop Sequence (25 分) Given a stack which can keep M numbers at most. Push N numbers in the ord ...