题目:

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

Example:

Input: [1,null,2,3]
1
\
2
/
3 Output: [3,2,1]

Follow up: Recursive solution is trivial, could you do it iteratively?

分析:

给定一棵二叉树,返回后序遍历。

递归方法很简单,即先访问左子树,再访问右子树,最后访问根节点。但题目说了你能用迭代解决吗?

思考这样一个方法,我们利用栈来模拟递归方法。

按给的样例说明后序遍历,我们时先访问根节点1的左子树,访问根节点1的右子树,再访问根节点1。

1: node1->left, node1->right, node1->val;

node1左子树为空,我们继续访问node1的右子树。

: node2->left, node2->right, node2->val, node1->val;

node2左子树为空,继续访问右子树

: node3->left, node3->right, node3->val, node2->val, node1->val;

node3左右子树都为空,结果就是[3, 2, 1]。

用栈模拟的话,递归顺序是node1,之后是node2,node3,实际上也就是根节点,右子树,左子树,根节点我们可以直接输出,左右子树节点压入栈时候要注意,先将左子树压入,再将右子树节点压入,这样可以保证下次迭代执行栈内节点时,先执行右子树。

最后得到的结果要反转一下。

程序:

//递归
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> res;
help(res, root);
return res;
}
void help(vector<int> &vec, TreeNode* root){
if(root == nullptr) return;
help(vec, root->left);
help(vec, root->right);
vec.push_back(root->val);
}
};
//迭代
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
if(root == nullptr) return {};
vector<int> res;
stack<TreeNode*> s;
s.push(root);
while(!s.empty()){
root = s.top();
s.pop();
res.push_back(root->val);
if(root->left) s.push(root->left);
if(root->right) s.push(root->right);
}
reverse(res.begin(), res.end());
return res;
}
};

LeetCode 145. Binary Tree Postorder Traversal二叉树的后序遍历 (C++)的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    给定一个二叉树,返回它的 后序 遍历. 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 递归: class Solution { public: vector<int> res; ve ...

随机推荐

  1. [日常] NOI前划水日记

    NOI前划水日记 开坑记录一下每天的效率有多低 5.24 早上被春哥安排了一场NEERC(不过怎么是qualification round啊) 省队势力都跑去参加THU/PKU夏令营了...剩下四个D ...

  2. 关于ios 11.X后微信wifi认证,无法打开微信,无法重定向到weixin:开头网址等问题的处理

    环境: 认证路由ROS ,认证后台python django ios11系统 更新以来先后出现微信wifi认证,无法打开微信,无法重定向到weixin:开头网址等相关问题. 经过问题的收集,查询到网络 ...

  3. [NewLife.XCode]导入导出(实体对象百变魔君)

    NewLife.XCode是一个有10多年历史的开源数据中间件,支持nfx/netcore,由新生命团队(2002~2019)开发完成并维护至今,以下简称XCode. 整个系列教程会大量结合示例代码和 ...

  4. Javal连载4-注释&class与public class区别

    一.Java注释 1.作用:不会编译倒.class文件之中:增强可读性 2.分类: (1)单行注释(只注释当前行):// (2)多行注释: /* 注释 注释 注释 */ (3)javadoc注释 /* ...

  5. centos 启动一个tcp服务程序

    需要先yum安装: yum install nc 启动服务: nc -l 80

  6. LeetCode 189:旋转数组 Rotate Array

    公众号:爱写bug(ID:icodebugs) 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. Given an array, rotate the array to the ...

  7. playtime-浙大羽协裁判部训练方案[随机事件序列的应用]

    首先随机一列人名 然后按比例随机一列事件项. 然后将不确定项的人名更正为“某人”[比如发球违例,,,你怎么知道谁在发球] 最后定义一个初始化. 初始化呢,就是挑边. 球权还是场权? 发球还是接发? 谁 ...

  8. Python自动抢红包,超详细教程,再也不会错过微信红包了!

    目录: 0 引言 1 环境 2 需求分析 3 前置准备 4 抢红包流程回顾 5 代码梳理 6 后记 0 引言 提到抢红包,就不得不提Xposed框架,它简直是个抢红包的神器,但使用Xposed框架有一 ...

  9. sap和OA之间数值传递1(环境准备)

    1.本公司使用的是致远A8,首先在本机上准备好A8环境,java环境(jre1.8.0_131),eclipse版本(建议用eclipseInstaller下载最新的NEON版本),安装致远ide插件 ...

  10. Windows Server 2008 R2 install Visual Studio 2015 failed

    Please download and install Windows Server 2008 R2 Service Pack 1 (KB976932) . https://www.microsoft ...