LeetCode 145. Binary Tree Postorder Traversal二叉树的后序遍历 (C++)
题目:
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++)的更多相关文章
- C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)
145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...
- [LeetCode] 145. Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- LeetCode 145. Binary Tree Postorder Traversal 二叉树的后序遍历 C++
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [,,] \ / O ...
- 【LeetCode】Binary Tree Postorder Traversal(二叉树的后序遍历)
这道题是LeetCode里的第145道题. 题目要求: 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很 ...
- leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历)
题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given bina ...
- 145 Binary Tree Postorder Traversal 二叉树的后序遍历
给定一棵二叉树,返回其节点值的后序遍历.例如:给定二叉树 [1,null,2,3], 1 \ 2 / 3返回 [3,2,1].注意: 递归方法很简单,你可以使用迭代方法来解 ...
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- lintcode:Binary Tree Postorder Traversal 二叉树的后序遍历
题目: 二叉树的后序遍历 给出一棵二叉树,返回其节点值的后序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [3,2,1] 挑战 你能使用非递归实现么? 解题: 递归程序 ...
- Leetcode145. Binary Tree Postorder Traversal二叉树的后序遍历
给定一个二叉树,返回它的 后序 遍历. 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 递归: class Solution { public: vector<int> res; ve ...
随机推荐
- 小程序setData()使用和注意事项
注意: 直接修改this.data,而不调用this.setData(),是无法改变当前页面的状态的,会导致数据不一致 仅支持可以JSON化的数据 单次设置的数据不能超过1024KB,尽量避免一次设置 ...
- MySQL实战45讲学习笔记:第四十三讲
一.本节概述 我经常被问到这样一个问题:分区表有什么问题,为什么公司规范不让使用分区表呢?今天,我们就来聊聊分区表的使用行为,然后再一起回答这个问题. 二.分区表是什么? 为了说明分区表的组织形式,我 ...
- 第09组 Beta版本演示
组长博客 本组(组名)所有成员 短学号 姓名 2236 王耀鑫(组长) 2210 陈超颖 2209 陈湘怡 2228 许培荣 2204 滕佳 2205 何佳琳 2237 沈梓耀 2233 陈志荣 22 ...
- 剑指offer:二叉树打印成多行(层次遍历)
1. 题目描述 从上到下按层打印二叉树,同一层结点从左至右输出.每一层输出一行. 2. 思路 层次遍历 3. 递归 public class Solution { ArrayList<Array ...
- cURL error 60: SSL certificate problem: unable to get local issuer certificate(转)【亲测】
php5.6以上的版本会出现这种问题 解决办法: [开启拓展] extension=curl extension=openssl [配置证书] 访问https://curl.haxx.se/docs/ ...
- Win10导出查看删除已安装的证书
1.控制面板中搜索证书 2.点击管理用户证书或管理计算机证书,即可查看所有安装好的证书 3.双击即可查看证书,右键证书即可删除无效的证书 4.搜索证书,比如百度用到的根证书是GlobalSign Ro ...
- matplotlib动态绘图
目录 package Process 解决中文乱码问题 simple_plot() scatter_plot() three_dimension_scatter() Jupyter notebook ...
- pixijs shader教程
pixijs 写shader 底层都封装好了 只要改改片段着色器就行了 pxijs一定刚要设置支持透明 不然 颜色不支持透明度了 const app = new PIXI.Application({ ...
- OpenGL入门1.3:着色器 GLSL
前言 经过之前一段时间的学习(渲染管线简介)我们已经知道了着色器(Shader)是运行在GPU上的程序,这些小程序为图形渲染管线的某个特定部分而运行,着色器只是一种把输入转化为输出的程序,着色器也是一 ...
- Solr单机版的安装与使用
.使用Solr实现. 基于Solr实现站内搜索扩展性较好并且可以减少程序员的工作量,因为Solr提供了较为完备的搜索引擎解决方案,因此在门户.论坛等系统中常用此方案. .什么是Solr. Solr是A ...