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)的更多相关文章

  1. [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 ...

  2. C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)

    145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...

  3. 二叉树前序、中序、后序非递归遍历 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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...

  4. LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)

    145. 二叉树的后序遍历 145. Binary Tree Postorder Traversal 题目描述 给定一个二叉树,返回它的 后序 遍历. LeetCode145. Binary Tree ...

  5. 【LeetCode】145. Binary Tree Postorder Traversal (3 solutions)

    Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...

  6. 12. Binary Tree Postorder Traversal && Binary Tree Preorder Traversal

    详见:剑指 Offer 题目汇总索引:第6题 Binary Tree Postorder Traversal            Given a binary tree, return the po ...

  7. LeetCode: Binary Tree Postorder Traversal 解题报告

    Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...

  8. 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 ...

  9. LeetCode:145_Binary Tree Postorder Traversal | 二叉树后序遍历 | Hard

    题目:Binary Tree Postorder Traversal 二叉树的后序遍历,题目要求是采用非递归的方式,这个在上数据结构的课时已经很清楚了,二叉树的非递归遍历不管采用何种方式,都需要用到栈 ...

随机推荐

  1. fatal: The remote end hung up unexpectedly解决办法

    $ git config --global http.postBuffer 2428000 git config http.postBuffer 524288000 配置完成后 git pull一下, ...

  2. Oracle 11gR2 RAC集群服务启动与关闭总结

    引言:这写篇文章的出处是因为我的一名学生最近在公司搭建RAC集群,但对其启动与关闭的顺序和原理不是特别清晰,我在教学工作中也发现了很多学员对RAC知识了解甚少,因此我在这里就把RAC里面涉及到的最常用 ...

  3. 基于spring的异常一站式解决方案

    https://segmentfault.com/a/1190000006749441#articleHeader4 https://lrwinx.github.io/2016/04/28/%E5%A ...

  4. Mac环境下PHPstorm配置xdebug开发调试web程序

    一.安装PHP的xdebug扩展 安装xdebug(技巧,为了找到适配的版本,让xdebug网站根据phpinfo()函数输出分析找到对应的方法及安装步骤:如果安装了多个PHP版本的话,尽量用phpi ...

  5. MxNet C++和python环境配置

    MxNet C++和python环境配置 安装文件: 1.为了与python已经安装好的版本一致,在这个网站下载mxnet 1.0.0的源码 https://github.com/apache/inc ...

  6. GOF23设计模式之桥接模式(bridge)

    一.桥接模式概述 桥接模式核心要点: 处理多层继承结构,处理多维度变化的场景,将各个维度设计成独立的继承结构,使各个维度可以独立的扩展在抽象层建立关联. 二.桥接模式场景提出与存在问题 商城系统中常见 ...

  7. 【学徒日记】Unity 动画调用事件

    http://note.youdao.com/noteshare?id=a15f965fc57a0b25c87ee09388cf0f4a 具体内容看上面的链接. 1. 在脚本里写一个函数,它的参数只能 ...

  8. Go - 常量与运算符

    常量的定义 1. 常量的值在编译的时候就已经确定.所以,在定义的时候就必须赋值 2. 使用 const 关键字来声明常量.赋值形式与变量类似: // 标准定义 const PI int = 3.14 ...

  9. Go - coding之前的准备

    Go tool 的使用 Go的tool要求我们对于code有一定的结构化组织和管理,下面我们就来一介绍他们: --GoPath environment variable:  顾名思义,环境变量,指定了 ...

  10. md5加密(3)---org.apache.commons.codec.digest.DigestUtils.md5Hex(input)

    import org.apache.commons.codec.digest.DigestUtils;String sig = DigestUtils.md5Hex("str")