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?

Subscribe to see which companies asked this question
递归当然很容易实现拉,但是要求是不使用递归来实现,先贴一个递归的代码:

 class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
if(!root) return ret;
tranverse(root);
return ret;
} void tranverse(TreeNode * root)
{
if(!root) return;
tranverse(root->left);
tranverse(root->right);
ret.push_back(root->val);
}
private:
vector<int> ret;
};

下面是非递归实现的代码,用一个栈来保存数据:
注意左右子树的压栈顺序

 /**
* 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> ret;
if(!root) return ret;
stack<TreeNode *> s;
s.push(root);
while(!s.empty()){
TreeNode * t = s.top();
if(!t->left && !t->right){
ret.push_back(t->val);
s.pop();
continue;
}
if(t->right){
s.push(t->right);
t->right = NULL;
}
if(t->left){
s.push(t->left);
t->left = NULL;
}
}
return ret;
}
};

LeetCode OJ:Binary Tree Postorder Traversal(后序遍历二叉树)的更多相关文章

  1. Leetcode 94. Binary Tree Inorder Traversal (中序遍历二叉树)

    Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...

  2. 094 Binary Tree Inorder Traversal 中序遍历二叉树

    给定一个二叉树,返回其中序遍历.例如:给定二叉树 [1,null,2,3],   1    \     2    /   3返回 [1,3,2].说明: 递归算法很简单,你可以通过迭代算法完成吗?详见 ...

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

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

  4. LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)

    翻译 给定一个二叉树.返回其兴许遍历的节点的值. 比如: 给定二叉树为 {1. #, 2, 3} 1 \ 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 ...

  5. (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal

    Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...

  6. [LeetCode] 145. Binary Tree Postorder Traversal 二叉树的后序遍历

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

  7. leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历)

    题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given bina ...

  8. LeetCode 145. Binary Tree Postorder Traversal 二叉树的后序遍历 C++

    Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [,,] \ / O ...

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

    题目: Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,nul ...

  10. 【LeetCode】Binary Tree Postorder Traversal(二叉树的后序遍历)

    这道题是LeetCode里的第145道题. 题目要求: 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很 ...

随机推荐

  1. leetcode -day 15 Distinct Subsequences

    1.  Distinct Subsequences  Given a string S and a string T, count the number of distinct subsequen ...

  2. DL for objection detection

    在计算机视觉领域,"目标检测"主要解决两个问题:图像上多个目标物在哪里(位置),是什么(类别).围绕这个问题,人们一般把其发展历程分为3个阶段:1. 传统的目标检测方法2. 以R- ...

  3. 基于mondrian聚合表的R计算olap开发

    原文出处:http://www.cnblogs.com/qiaoyihang/p/7348328.html 最近在做基于Mondrian的olap开发,总结一下! 一. schema构建 1.思考:我 ...

  4. phpcms 列表页中,如何调用其下的所有子栏目?

    {pc:content action="category" catid="$catid" num="99" order="list ...

  5. 10046 trace详解(1)

    10046 trace帮助我们解析一条/多条SQL.PL/SQL语句的运行状态,这些状态包括:Parse/Fetch/Execute三个阶段中遇到的等待事件.消耗的物理和逻辑读.CPU时间.执行计划等 ...

  6. Canvas:绘制路径

    Canvas:绘制路径 绘制路径 图形的基本元素是路径.路径是[通过不同颜色和宽度的线段或曲线相连形成的不同形状的]点的集合.一个路径,甚至一个子路径,都是闭合的. 使用路径绘制图形需要一些额外的步骤 ...

  7. C# 异步同步调用

    本文将主要通过“同步调用”.“异步调用”.“异步回调”三个示例来讲解在用委托执行同一个“加法类”的时候的的区别和利弊. 首先,通过代码定义一个委托和下面三个示例将要调用的方法: /*添加的命名空间 u ...

  8. 【JavaScript】用JS绘制一个球

    参考: 1.http://www.w3school.com.cn/html5/html_5_canvas.asp 2.http://blog.csdn.net/qq_27626333/article/ ...

  9. pt-osc使用方法

    pt-osc实战运用 1.安装pt-osc,解压即可用 安装包在:10.135.2.217:data/online/software/percona-toolkit-3.0.12.tar.gz tar ...

  10. CSS3自定义checkbox复选框

    在线演示 本地下载