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. Xamrin开发安卓笔记(二)

    http://www.cnblogs.com/minCS/p/4112617.html Xamrin开发安卓笔记(二)   安装篇 Xamrin开发安卓笔记(一) 昨天调理一天AAPT.EXE 被推出 ...

  2. Java集合(4):Iterator(迭代器)

    迭代器是一种设计模式,它是一个对象,它可以遍历并选择序列中的对象,而开发人员不需要了解该序列的底层结构.迭代器通常被称为“轻量级”对象,因为创建它的代价小. Java中的Iterator功能比较简单, ...

  3. ReportView显示本地报表

    from:http://www.cnblogs.com/duanshuiliu/archive/2012/07/13/2589862.html 使用ReportView控件可以显示远端Report s ...

  4. 用cmd运行php代码、socket

    一.用cmd运行php代码,首先要对电脑进行配置: 1.右击计算机-属性-高级系统设置-环境变量,我们需要添加环境变量. 2.在Administrator的用户变量(U)下点击新建,弹出对话框,变量名 ...

  5. PAT 天梯赛 L1-003. 个位数统计

    题目链接 https://www.patest.cn/contests/gplt/L1-003 题意 计算每个不同数字出现的次数 思路 可以用 MAP标记, 也可以直接用数字 存 AC代码 #incl ...

  6. Http请求的TCP连接

    我们一直认为,HTTP连接分为长连接和短连接,而我们现在常用的都是HTTP1.1,因此我们用的都是长连接. 这句话其实只对了一半,我们现如今的HTTP协议,大部分都是1.1的,因此我们平时用的基本上都 ...

  7. viewport大白话

    以下所有内容均是我自己理解的,可能有误,懂得大佬希望指点一下我.. 首先,写一个简单的页面.里面只有1个200*200的div <html lang="en"> < ...

  8. Oracle函数如何把符串装换为小写的格式

    我们都知道Oracle函数在实际的应用中比较广泛,对其的实际操作与其相关功能也是颇为熟悉,但是你了解Oracle函数怎样使将字符串装换为小写的格式的具体操作吗?如果你有兴趣的话你就可以浏览以下的文章. ...

  9. PHP的date函数的时区问题

    来自:http://www.cnblogs.com/fuland/p/4250462.html(“腐烂的翅膀”的博客) 从php5.1.0开始,php.ini里加入了date.timezone这个选项 ...

  10. java jvm内存管理/gc策略/参数设置

    1. JVM内存管理:深入垃圾收集器与内存分配策略 http://www.iteye.com/topic/802638 Java与C++之间有一堵由内存动态分配和垃圾收集技术所围成的高墙,墙外面的人想 ...