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 ...
随机推荐
- arduino (3) 控制sim900A发送短信
狗屎佳世通旗舰店,卖的什么破玩意sim900a芯片,不支持联通卡,还生明模块支持双卡的 之前买的esp8266-07都是内存偷工减料 买的液体浊度传感器给的原理图也不给基本接线. 差评垃圾店,你敢卖就 ...
- A1044 Shopping in Mars (25 分)
一.技术总结 可以开始把每个数都直接相加当前这个位置的存放所有数之前相加的结果,这样就是递增的了,把i,j位置数相减就是他们之间数的和. 需要写一个函数用于查找之间的值,如果有就放返回大于等于这个数的 ...
- [LOJ 6432][PKUSC 2018]真实排名
[LOJ 6432][PKUSC 2018]真实排名 题意 给定 \(n\) 个选手的成绩, 选中其中 \(k\) 个使他们的成绩翻倍. 对于每个选手回答有多少种方案使得他的排名不发生变化. \(n\ ...
- 【shell脚本】批量修改扩展名===modifyExtension.sh
前提:需切换到需要批量修改扩展名的目录下,运行脚本 [root@VM_0_10_centos shellScript]# cat modifyExtension.sh #!/bin/bash # 编写 ...
- linq 获取不重复数据,重复数据 var unique = arr.GroupBy(o => o).Where(g => g.Count() == 1) .Select(g => g.ElementAt(0));
static void Main(string[] args) { int[] arr = { 1, 3, 3, 3, 3, 4, 5, 4, 5, 8, 9, 3 }; //不重复 var uniq ...
- .Net MVC 提示未能加载文件或程序集
最近在开发.Net MVC程序时,突然出现未能加载文件或程序集的错误, 错误1 错误2 猜测时由于引用了Swagger,导致Swagger依赖的组件版本和现有版本冲突(现在仍未确定是这个原因),浪费了 ...
- python 父类方法中使用不同的子类中的不同类对象
# coding:utf-8 class Animal(object): def __init__(self): self._name = None self._f = None def eat(se ...
- Python游戏开发——打砖块
打砖块游戏向来大家也不会很陌生,今天来用python来开发一下这个小游戏 1.引用对应数据库 import pygame from pygame.locals import * import sys, ...
- deepin把vscode设为默认文本应用
一开始我想自己写一个desktop文件放在/usr/share/applications下面,结果在右键菜单里面找不到vscode. [Desktop Entry] Categories=Develo ...
- Scrum 冲刺第四篇
我们是这次稳了队,队员分别是温治乾.莫少政.黄思扬.余泽端.江海灵 一.会议 1.1 28号站立式会议照片: 1.2 昨天已完成的事情 团队成员 昨日已完成的任务 黄思扬 活动内容管理页(前端) ...