给定一个二叉树,返回它的 后序 遍历。

进阶: 递归算法很简单,你可以通过迭代算法完成吗?

递归:

class Solution {
public:
vector<int> res;
vector<int> postorderTraversal(TreeNode* root) {
if(root == NULL)
return res;
postorderTraversal(root ->left);
postorderTraversal(root ->right);
res.push_back(root ->val);
return res;
}
};

迭代:

方法一:

class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
stack<TreeNode*> s;
s.push(root);
vector<int> res;
if(root == NULL)
return res;
while(!s.empty())
{
TreeNode* temp = s.top();
if(temp->left)
{
s.push(temp->left);
temp->left = NULL;
}
else if(temp->right)
{
s.push(temp->right);
temp->right = NULL;
}
else
{
res.push_back(temp->val);
s.pop();
}
}
return res;
}
};

方法二:

class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> res;
if(root == NULL)
return res;
stack<TreeNode *> s;
s.push(root);
while(!s.empty())
{
TreeNode* node = s.top();
s.pop();
res.push_back(node ->val);
if(node ->left)
s.push(node ->left);
if(node ->right)
s.push(node ->right);
} return vector<int>(res.rbegin(), res.rend());
}
};

Leetcode145. Binary Tree Postorder Traversal二叉树的后序遍历的更多相关文章

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

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

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

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

  3. lintcode:Binary Tree Postorder Traversal 二叉树的后序遍历

    题目: 二叉树的后序遍历 给出一棵二叉树,返回其节点值的后序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [3,2,1] 挑战 你能使用非递归实现么? 解题: 递归程序 ...

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

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

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

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

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

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

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

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

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

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

  9. 145 Binary Tree Postorder Traversal 二叉树的后序遍历

    给定一棵二叉树,返回其节点值的后序遍历.例如:给定二叉树 [1,null,2,3],   1    \     2    /   3返回 [3,2,1].注意: 递归方法很简单,你可以使用迭代方法来解 ...

随机推荐

  1. [笔记]Laravel TDD 胡乱记录

    TDD: 测试驱动开发(Test-Driven Development),TDD的原理是在开发功能代码之前,先编写单元测试用例代码,测试代码确定需要编写什么产品代码. -- 载自TDD百度百科 参考 ...

  2. 6.RDD算子实战

    from pyspark import SparkContext,SparkConf import sys if __name__ == '__main__': if len(sys.argv) != ...

  3. (转)第05节:Fabric.js的动画设置

    凡是出色的Canvas库都少不了制作动画的方法,Fabric.js也不例外,它有着编写简单且功能强大的动画助手,这就是animate( )方法. animate主要使用代码如下: rect.anima ...

  4. 类的反射实例(servlet的抽取)

    类的反射实例 具体以后我们写的时候不用写BaseServlet,因为各种框架都已经给我们写好了 所以,user对应的servlet的界面长这样:

  5. c# 中xml序列化时相同节点存入不同类型值

    先上需要序列话的类定义: [System.Xml.Serialization.XmlIncludeAttribute(typeof(DescriptionType))] [System.CodeDom ...

  6. error C2872: 'ULONG_PTR' : ambiguous symbol

    转自VC错误:http://www.vcerror.com/?p=74 问题描述: 错误:error C2872: 'ULONG_PTR' : ambiguous symbol 解决方法: 详细的解决 ...

  7. 让delphi2010能有delphi7的版面布局

    如何让delphi2010能有delphi7的版面布局呢?答案是肯定的,方法如下: 1)在工具栏点右键,选择components 会看到delphi7中的组件显示面板 2)tools > opt ...

  8. POJ-1976-A Mini Locomotive-dp

    A train has a locomotive that pulls the train with its many passenger coaches. If the locomotive bre ...

  9. Spring+SpringMVC+Mybatis搭建的网站的处理流程总结

    最近学习了如何使用SSM框架搭建网站,以前没用过框架,第一次使用,总结一下自己对框架处理流程的理解

  10. css3@media实现原理

    window.matchMedia() 基本用法 window.matchMedia方法用来检查CSS的mediaQuery语句.各种浏览器的最新版本(包括IE 10+)都支持该方法,对于不支持该方法 ...