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 ...
随机推荐
- Python进阶-XVI 继承 单继承 多继承
一.初识继承 1.引入继承 class A(object): pass # 父类,基类,超类 class B: pass # 父类,基类,超类 class A_son(A, B): pass # 子类 ...
- 转:对softmax讲解比较清楚的博客
https://blog.csdn.net/wgj99991111/article/details/83586508
- jQuery 源码解析(六) $.each和$.map的区别
$.each主要是用来遍历数组或对象的,例如: var arr=[11,12,13,14]; $.each(arr,function(element,index){ //遍历arr数组 console ...
- linq 大数据 sql 查询及分页优化
前提: 需要nuget PredicateLib 0.0.5: SqlServer 2008R2 (建议安装 64 位): .net 4.5 或以上: 当前电脑配置: I7 4核 3.6G ...
- Docker学习(六)-Kubernetes - Spring Boot 应用
接上一篇 https://www.cnblogs.com/woxpp/p/11872155.html 新建 k8s-demo.yaml apiVersion: apps/v1beta2 kind: D ...
- System.InvalidOperationException: 'Cannot create more than one System.Windows.Application instance in the same AppDomain.'
System.Windows.Application is a singleton: its constructor must only be invoked once (including App. ...
- 以STM32和FPGA为核心的多组件协调工作系统
- selenium简单使用
简介 Selenium是一个用于Web应用程序测试的工具.Selenium可以直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE(7, 8, 9, 10, 11),Mozilla Fi ...
- 关于css中布局遇到的一些问题
现在本人初学网页布局经常遇到一些布局问题比如图片错位. 遇到的问题以及解决方案如下 行内元素有缝隙 块级元素没有缝隙 行内块元素中间会有小缝隙 常见的解决办法就是浮动
- Arduino leonardo+esp8266-01作服务端与APP进行数据通信
esp8266-01调试 一.硬件设备 1.USB转TTL 2.esp8266-01 3.杜邦线 4.电脑 二.接线 ESP8266 TTL-USB VCC VCC(最好选择3.3V) CH_PD V ...