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 二叉树的后序遍历,题目要求是采用非递归的方式,这个在上数据结构的课时已经很清楚了,二叉树的非递归遍历不管采用何种方式,都需要用到栈 ...
随机推荐
- 使用neon 开发nodejs addon
备注:开发使用的是mac 系统,需要安装rust nodejs .python2.7 Xcode 1. 安装neon npm install -g neon-cli 2. 创建简单项目 neon ...
- PHP----重置阿里云主机的密码
登陆阿里云,找到你的服务器 点击更多,选择重置密码,根据提示就可以了 这个密码用于连接FTP工具和SSH工具
- java之集合概述
集合也称容器:从大的类别分成两类:Collection和Map,也即:单列和双列列表. java编程思想中一张图说明该体系的整体结构:其中黑色着重的类是经常使用的类. 1 Collection Col ...
- shell三剑客之find
查找以ini结尾的文件[root@iZj6cbstl2n6r280a27eppZ app]# find / -name "*.ini"/app/myblog/config.ini ...
- 杂项: Memcached
ylbtech-杂项: Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动 ...
- java,js 解unicode
import java.util.regex.Matcher; import java.util.regex.Pattern; public class UnicodeDecodeDataConver ...
- 详解AJAX核心中的XMLHttpRequest对象
转自:http://developer.51cto.com/art/200904/119577.htm XMLHttpRequest 对象是AJAX功能的核心,要开发AJAX程序必须从了解XMLHtt ...
- canvas之画矩形
<canvas id="canvas" width="600" height="500" style="background ...
- [CSAPP] Chapter 1 Overview of Computer
1.1 information is bits + context All computer programs are just a sequence of bits, each with a val ...
- linux find中的-print0和xargs中-0的奥妙
默认情况下, find 每输出一个文件名, 后面都会接着输出一个换行符 ('n'), 因此我们看到的 find 的输出都是一行一行的: 比如我想把所有的 .log 文件删掉, 可以这样配合 xargs ...