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

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

递归:

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. yum -y install python-devel

    yum -y install python-devel的时候报错如图: Could not retrieve mirrorlist http://mirrorlist.centos.org/?rele ...

  2. leetcood学习笔记-45-跳跃游戏二

    题目描述: 第一次提交;超时 class Solution: def jump(self, nums: List[int]) -> int: l = [] for i in range(len( ...

  3. Java 基础 - 内存泄露Memory leak & 内存溢出Out of memory

    内存泄露 & 内存溢出 关系 https://www.cnblogs.com/panxuejun/p/5883044.html 内存泄露的6种情况: https://blog.csdn.net ...

  4. [JZOJ3320] 【BOI2013】文本编辑器

    题目 题目大意 给你一个文本,要删去其中所有的'e'. 有三种操作: h光标左移. x删除光标上面的字母(光标是横着的). fc跳到后面的第一个字符为'c'的位置. 问操作序列的最短长度. 思考历程 ...

  5. thinkphp 自动加载

    在3.2中,基本上无需手动加载类库文件,你可以很方便的完成自动加载. 命名空间自动加载 系统可以通过类的命名空间自动定位到类库文件,例如: 我们定义了一个类 Org\Util\Auth 类: name ...

  6. JSOI 2008 魔兽地图

    题目描述 DotR (Defense of the Robots) Allstars是一个风靡全球的魔兽地图,他的规则简单与同样流行的地图DotA (Defense of the Ancients) ...

  7. jq+baiduTemplate城市选择

    根据输入内容,动态匹配全国城市,如下图: 文件下载地址:chooseCity

  8. HDU-1852-Beijing 2008-一个神奇的公式求逆元

    As we all know, the next Olympic Games will be held in Beijing in 2008. So the year 2008 seems a lit ...

  9. 获取一个数组(vector)与查找值(value)的差最小绝对值的成员索引的算法

    代码如下: 函数作用:传递进来一个数组(vector),和一个需要查找的值(value),返回与value的差值绝对值最小的vector成员索引,若value不在vector范围中,则返回-1: in ...

  10. QString组合、拆分。

    1.组合字符常用arg()函数 QString test=QString("_haha_%1_hehe%2") .arg("ee").arg("aa& ...