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. An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full.

    与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误.未找到或无法访问服务器.请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接. (provider: TCP ...

  2. android studio 中类似VS的代码折叠功能Region

    1. 打开android studio 2. 选择要折叠的代码 3. 按Ctrl + Alt + T 选择 “region .. end region comments” Group selectio ...

  3. MariaDB备份之XtraBackup

    一.XtraBackup是由percona提供的mysql数据库备份工具,据官方介绍,这也是世界上惟一一款开源的能够对innodb和xtrabd数据库进行热备的工具.特点: (1)备份过程快速.可靠: ...

  4. python爬取当当网的书籍信息并保存到csv文件

    python爬取当当网的书籍信息并保存到csv文件 依赖的库: requests #用来获取页面内容 BeautifulSoup #opython3不能安装BeautifulSoup,但可以安装Bea ...

  5. Hadoop的eclipse1.1.2插件的安装和配置

    我的集群使用的hadoop版本是hadoop-1.1.2.对应的eclipse版本也是:hadoop-eclipse-plugin-1.1.2_20131021200005 (1)在eclipse的d ...

  6. mysql忽略一些错误代码

    模拟的故障,在从库中新建一个库,然后主库新建一个与从库相同名字的库,然后进入下面的show Mysql从库复制故障解决 当show slave status:报错 slave_io_running:y ...

  7. Log4Net各参数API

    <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSe ...

  8. Django_内置Admin

    Django内置Admin   Django内置的Admin是对于model中对应的数据表进行增删改查提供的组件,使用方式有: 依赖APP: django.contrib.auth django.co ...

  9. redis 系列文章推荐

    推荐博客: Redis在linux上的安装: http://www.open-open.com/lib/view/open1426468117367.html Redis的三种启动方式: http:/ ...

  10. 较常用的Math方法及ES6中的扩展

    记录下与Math有关的常用方法,如:求最大值.最小值等,或者是保留几位数啥的 1.数据 let floatA = 2.325232; let floatB = 2.3456; let temporar ...