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 二叉树的后序遍历,题目要求是采用非递归的方式,这个在上数据结构的课时已经很清楚了,二叉树的非递归遍历不管采用何种方式,都需要用到栈 ...
随机推荐
- webpack 图片资源处理
备注: css 引用图片资源 1. 安装loader yarn add file-loader --dev 2. 配置 const path = require("path"); ...
- nginx fastcgi 优化
fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2 keys_zone=TEST:10m inactive=5m; fastcgi ...
- PhpStorm 2017.3 版本在 Mac 系统 macOS High Sierra 版本 10.13.3 中运行很卡顿
最近升级了系统,发现PHPStorm 运行一会儿就卡顿起来了,按网上的方法加大内存配置也是没效果: 运行一会儿照样卡顿,接着一会儿就要内存溢出了挂掉了: 想着最近只有升级过操作系统,并没有升级JDK等 ...
- Java中如何查看一个类依赖的包
Java中如何查看一个类依赖的包 如图, 我如何知道JSONArray是依赖的哪一个包呢,这里有两个json-lib包? 测试语句: public static void main(Strin ...
- print 和 println的区别
println 输出字符后,下一个输出的字符会换行展示 print 输出字符后,下一个输出字符不会会换展示
- 初学java记录
记录一: if语句: if(x < y) System.out.println("x is less than y"); 记录二: 强制转换字符类型赋值的方法: num2= ...
- 后台判断ajax请求的请求后字段
headers设置:X-Requested-With:XMLHttpRequest 后台我就是根据这个来判断的
- python学习 (三十三) Modules
1: 方法一: 导入整个模块 import math class ModulesDemo(): def builtin_modules(self): print(math.sqrt()) m = Mo ...
- java程序中的ibatis连接mySql的基本实例
属性文件:SqlMap.properties driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/ibatis username= ...
- 10g RAC常用开启关闭命令 – SRVCTL用法
10G RAC: srvctl 1. Grammar: srvctl <command> <object> [<options]] available <comma ...