145. Binary Tree Postorder Traversal (Stack, Tree)
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?
class Solution {
public:
vector<int> postorderTraversal(TreeNode *root) {
vector<int> result;
if(!root) return result; stack<MyNode*> treeStack;
MyNode* myRoot = new MyNode(root);
MyNode* current, *newNode;
treeStack.push(myRoot); while(!treeStack.empty())
{
current = treeStack.top();
treeStack.pop();
if(!current->flag)
{
current->flag = true;
treeStack.push(current);
if(current->node->right) {
newNode = new MyNode(current->node->right);
treeStack.push(newNode);
}
if(current->node->left) {
newNode = new MyNode(current->node->left);
treeStack.push(newNode);
}
}
else
{
result.push_back(current->node->val);
}
}
return result; }
struct MyNode
{
TreeNode* node;
bool flag; //indicate if the node has been visited
MyNode(TreeNode* x) : node(x),flag(false) {}
};
};
法II: 不重新定义结构,以root->right->left的顺序访问节点,最后逆序。
/**
* 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> postorderTraversal(TreeNode* root) {
vector<int> result;
if(!root) return result; stack<TreeNode*> treeStack;
TreeNode* current;
treeStack.push(root); //visit in order root->right->left
while(!treeStack.empty())
{
current = treeStack.top();
treeStack.pop();
result.push_back(current->val);
if(current->left) treeStack.push(current->left);
if(current->right) treeStack.push(current->right);
} //reverse result
int size = result.size();
int hsize = size>>;
int tmp;
for(int i = ; i < hsize; i++){
tmp = result[i];
result[i]=result[size--i];
result[size--i]=tmp;
}
return result;
}
};
145. Binary Tree Postorder Traversal (Stack, Tree)的更多相关文章
- [LeetCode] N-ary Tree Postorder Traversal N叉树的后序遍历
Given an n-ary tree, return the postorder traversal of its nodes' values. For example, given a 3-ary ...
- C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)
145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...
- 二叉树前序、中序、后序非递归遍历 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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
- LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)
145. 二叉树的后序遍历 145. Binary Tree Postorder Traversal 题目描述 给定一个二叉树,返回它的 后序 遍历. LeetCode145. Binary Tree ...
- 【LeetCode】145. Binary Tree Postorder Traversal (3 solutions)
Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...
- 12. Binary Tree Postorder Traversal && Binary Tree Preorder Traversal
详见:剑指 Offer 题目汇总索引:第6题 Binary Tree Postorder Traversal Given a binary tree, return the po ...
- LeetCode: Binary Tree Postorder Traversal 解题报告
Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...
- 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 ...
- LeetCode:145_Binary Tree Postorder Traversal | 二叉树后序遍历 | Hard
题目:Binary Tree Postorder Traversal 二叉树的后序遍历,题目要求是采用非递归的方式,这个在上数据结构的课时已经很清楚了,二叉树的非递归遍历不管采用何种方式,都需要用到栈 ...
随机推荐
- testem方便的web tdd 测试框架使用
备注: 单元测试,对于日常的开发是比较重要的,testem 简化了我们的代码编写,以及运行. 主要特性: a. 支持的测试框架有:jasmine quint mocha buster ...
- web 调试工具docker的安装使用
1. weinre 工具 docker run -d -p 8080:8080 beevelop/weinre 2. vorlonjs(不支持https) docker run --name v ...
- cookie添加中文encoding/decoding
添加时编码,读取是解码,否则编译不过 Cookie cookie=new Cookie("姓名","黄嘎兵"); respose.addCookie(cooki ...
- Android.mk语法解析
Android.mk 相当于 Linux 中的 Makefile 文件,用来向安卓系统描述如何编译源代码.该文件会被编译器解析多次,所以尽量减少在 Android.mk 中声明变量. Android. ...
- MVC自定义错误页面
MVC异常处理主要有三种方案:1.基于HandleErrorAttribute重写OnException方法:2.基于Global.apsx添加Application_Error方法:3.直接在Web ...
- Python文件操作,with open as追加文本内容实例
最常见的读写操作 import re with open('/Users/Mr.Long/Desktop/data.txt', 'w') as f: f.write('hello world') 就这 ...
- 第三章 Istio基本介绍
3.1 Istio的核心组件及其功能 Istio总体分两部分:控制面和数据面. 数据面(sidecar):sidecar通过注入的方式和业务容器共存于一个pod,会劫持业务容器的流量,并接受控制面组件 ...
- kubernetes 学习 service相关
1: service有什么用? 直接通过Pod的IP地址和端口号可以访问容器应用,但是pod的IP地址是不可靠的,比如POD出现故障后,有可能在另外一个NOde上启动,这样Pod的IP ...
- 使用nginx反向代理进行负载均衡
在这里简单记录一下,我使用Nginx反向代理进行负载均衡,将请求发送到两台tomcat上. 首先解压两个tomcat,解压Nginx,一台tomcat配置可以不用动,但是我为了更方便只是将它的端口改为 ...
- 线程之死锁、递归锁、信号量、事件Event 、定时器
1.死锁的现象 所谓死锁: 是指两个或两个以上的进程或线程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力作用,它们都将无法推进下去.此时称系统处于死锁状态或系统产生了死锁,这些永远在互相 ...