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 ...
随机推荐
- 支持Android iOS,firefox(其它未测)的图片上传客户端预览、缩放、裁切。
var version = '007'; var host = window.location.host; function $$(id){return document.getElementById ...
- SVN - 忽略已经提交的文件
1.在本地删除要忽略的文件 2.与资源库同步,提交删除的文件 3.忽略文件
- RoseRT 建模学习
目录: 一.RoseRT理论知识 二.一个完整模型的建立 三.TD-SCDMA(UE侧)RRC层建模的学习 四.LTE的RRC层建模(1.自主完成‘2.也可以是L2) 五.参考文献 一.RoseRT理 ...
- android 使用相机拍照,并存储到手机sd卡上,并利用系统录像录像并播放
//首先声明一个成员变量 String savePath,用来储存文件路径 /** * 保存照片路径 * @return 返回图片的一个文件 * @throws IOException 抛出一个异常 ...
- Service中事务不能回滚的解决方式(转)
1.在service方法里面如果对异常进行了捕获的话,该事务是不会进行回滚的 默认spring事务只在发生未被捕获的 runtimeexcetpion时才回滚. spr ...
- js限制文本框只能输入整数或者带小数点[转]
这篇文章是关于js限制文本框只能输入整数或者带小数点的内容,以下就是该内容的详细介绍. 做表单验证的时候是否会碰到验证某个输入框内只能填写数字呢,仅允许输入整数数字或者带小数点的数字.下面这段代码也许 ...
- UIkit框架之UIPickerView
1.继承链:UIview:UIResponder:NSObject 2.获取uipicker view的属性 (1)@property(nonatomic, readonly) NSIntegernu ...
- Nested List Weight Sum -- LeetCode 339
Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...
- Canvas旋转元素
Canvas是HTML5的画布元素,有时需要按指定角度旋转某一个元素. var canvas = document.getElementById("mycanvas"); var ...
- Java多线程之ConcurrentSkipListMap深入分析(转)
Java多线程之ConcurrentSkipListMap深入分析 一.前言 concurrentHashMap与ConcurrentSkipListMap性能测试 在4线程1.6万数据的条件下, ...