12. Binary Tree Postorder Traversal && Binary Tree Preorder Traversal
详见:剑指 Offer 题目汇总索引:第6题
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?
注:后序遍历是较麻烦的一个,不可大意。关键两点: 1.要走到 p->left | p->right ==0, 2.每次出栈出两个结点。
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> postorderTraversal(TreeNode *root) {
vector<int> answer;
if(root == NULL) return answer;
stack<TreeNode*> st;
st.push(root);
TreeNode *p = root;
while(p->right || p->left) {
while(p->left) { st.push(p->left); p = p->left;}
if(p->right) { st.push(p->right); p = p->right;}
}
while(!st.empty()) {
TreeNode *q = st.top(); st.pop();
answer.push_back(q->val);
if(!st.empty()) {
TreeNode *q2 = st.top();
while(q2->right && q2->right != q) {
st.push(q2->right); q2 = q2->right;
while(q2->left || q2->right) {
while(q2->left){ st.push(q2->left); q2 = q2->left;}
if(q2->right){ st.push(q2->right); q2 = q2->right;}
}
}
}
}
return answer;
}
};
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 binary tree
* 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> vec;
if(root == NULL) return vec;
stack<TreeNode*> st;
st.push(root);
while(!st.empty()) {
TreeNode *p = st.top(); st.pop();
vec.push_back(p->val);
if(p->right) st.push(p->right);
if(p->left) st.push(p->left);
}
return vec;
}
};
12. Binary Tree Postorder Traversal && Binary Tree Preorder Traversal的更多相关文章
- [Swift]LeetCode1008. 先序遍历构造二叉树 | Construct Binary Search Tree from Preorder Traversal
Return the root node of a binary search tree that matches the given preorder traversal. (Recall that ...
- LeetCode 1008. Construct Binary Search Tree from Preorder Traversal
原题链接在这里:https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/ 题目: Retu ...
- 【leetcode】1008. Construct Binary Search Tree from Preorder Traversal
题目如下: Return the root node of a binary search tree that matches the given preorder traversal. (Recal ...
- 【LeetCode】1008. Construct Binary Search Tree from Preorder Traversal 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- 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 ...
- 二叉树前序、中序、后序非递归遍历 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator
144. Binary Tree Preorder Traversal 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
- Binary Tree Postorder Traversal leetcode java
题目: Given a binary tree, return the postorder traversal of its nodes' values. For example: Given bin ...
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- [LeetCode] Binary Tree Preorder Traversal 二叉树的先序遍历
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
随机推荐
- spark0.9.1 assembly build-RedHat6.4 YARN 2.2.0
1. Install git on RedHat6.4: 1.1. setup your local yum repo 1.2. yum install git 2. Install JDK and ...
- Introducing Windows 10 Editions(Windows10版本介绍)
Windows 10将在今年夏天正式发布,今天微软官方博客分享了一些Windows 10版本的细节.详见Introducing Windows 10 Editions Windows 10 HomeW ...
- Tri-Training: Exploiting Unlabeled Data Using Three Classifiers
Abstract – In many practical data mining applications such as web page classification, unlabeled tra ...
- 常用的 SQL 函数
SQL 函数 聚合函数(针对数字列): AVG:求平均分 COINT: 计算个数 MAX: 求最大值 MIN: 求最小值 SUM: 求和 数学函数(): ABS: 绝对值 CEIL ...
- IOS 在控制器间跳转实现过渡动画
已经掌握了CALayer下的CATransition动画在同一个控制器下实现,但是在不同控制器间跳转又该如何实现呢? MyViewController *myVC = [[MyViewControll ...
- C# WinForm 应用程序 开启Console窗口
/********************************************************************************* * C# WinForm 应用程序 ...
- sqlite入门
SQLite官网: https://www.sqlite.org/index.html 1. 下载请到https://www.sqlite.org/download.html下载相应平台的sqlite ...
- android4.x获取(也可监测)外置sd路径和读写
先上图: 这个小demo是判断手机上是否插入了sd卡(手动插入到手机卡槽的情况),如果拔出sd卡,也会检测到,检测到没有sd的话会提示退出.大家可以修改代码达到自己想要的效果. sd的卡装载状态是从系 ...
- Can 't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock '(2) ;
Can 't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock '(2) ; 如果你在网上search这个 ...
- 第七课 第二节,T语言流程语句(版本5.0)
while语句 循环结构是程序中一种很重要的结构其特点是,在给定条件成立时,反复执行某程序段,直到条件不成立为止给定的条件称为循环条件,反复执行的程序段称为循环体 (注:关键字,while,end) ...