当前位置:网站首页>【LeetCode】145.二叉树的后序遍历
【LeetCode】145.二叉树的后序遍历
2022-08-02 02:40:00 【酥酥~】
题目
给你一棵二叉树的根节点 root ,返回其节点值的 后序遍历 。
示例 1:
输入:root = [1,null,2,3]
输出:[3,2,1]
示例 2:
输入:root = []
输出:[]
示例 3:
输入:root = [1,2,3,4,5,6,7]
输出: [4,5,2,6,7,3,1]
提示:
树中节点的数目在范围 [0, 100] 内
-100 <= Node.val <= 100
进阶:递归算法很简单,你可以通过迭代算法完成吗?
题解
使用深度优先遍历
使用递归方法
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */
class Solution {
public:
void fun(TreeNode* node,vector<int> &result)
{
if(node->left)
fun(node->left,result);
if(node->right)
fun(node->right,result);
result.emplace_back(node->val);
}
vector<int> postorderTraversal(TreeNode* root) {
vector<int> result;
if(root)
fun(root,result);
return result;
}
};
使用迭代方法
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> result;
stack<TreeNode*> mystack;
TreeNode* node = root;
TreeNode* pre = nullptr;
while(!mystack.empty() || node!=nullptr)
{
while(node!=nullptr)
{
mystack.emplace(node);
node = node->left;
}
node = mystack.top();
mystack.pop();
if(node->right && node->right!=pre)
{
mystack.emplace(node);
node = node->right;
}
else
{
result.emplace_back(node->val);
pre = node;
node = nullptr;
}
}
return result;
}
};
边栏推荐
- The first time I wrote a programming interview question for Niu Ke: input a string and return the letter with the most occurrences of the string
- BioVendor Human Club Cellular Protein (CC16) Elisa Kit Research Fields
- 2022牛客多校四_G M
- 菜刀webshell特征分析
- 架构:分布式任务调度系统(SIA-Task)简介
- How engineers treat open source
- nacos startup error, the database has been configured, stand-alone startup
- EasyGBS平台播放视频时偶尔出现播放失败是什么原因?
- C#测试项目中属性的用法
- AWR analysis report questions for help: How can SQL be optimized from what aspects?
猜你喜欢

KICAD 拉线宽度无法修改,解决方法

Nanoprobes Polyhistidine (His-) Tag: Recombinant Protein Detection Protocol

2022-08-01 mysql/stoonedb slow SQL-Q18 analysis

Service discovery of kubernetes

搭建zabbix监控及邮件报警(超详细教学)

极大似然估计

Flask之路由(app.route)详解
![[Unity entry plan] 2D Game Kit: A preliminary understanding of the composition of 2D games](/img/8a/07ca69c6dcc22757156cb615e241f8.png)
[Unity entry plan] 2D Game Kit: A preliminary understanding of the composition of 2D games

2022 Henan Youth Training League Game (3)

KICAD 小封装拉线卡顿问题 解决方法
随机推荐
【每日一道LeetCode】——9. 回文数
Safety (1)
2022-08-01 mysql/stoonedb slow SQL-Q18 analysis
Outsourcing worked for three years, it was abolished...
Nanoprobes丨1-mercapto-(triethylene glycol) methyl ether functionalized gold nanoparticles
OC中成员变量,实例变量和属性之间的区别和联系
Docker-compose安装mysql
微服务:微智能在软件系统的简述
AWR analysis report questions for help: How can SQL be optimized from what aspects?
2022-08-01 mysql/stoonedb慢SQL-Q18分析
数仓:数仓从ETL到ELT架构的转化以及俩者的区别
The failure to create a role in Dahua Westward Journey has been solved
ofstream,ifstream,fstream read and write files
Talking about the "horizontal, vertical and vertical" development trend of domestic ERP
cocos中使用async await异步加载资源
Flask之路由(app.route)详解
灰度传感器、、、diy原理。。图
数仓:为什么说 ETL 的未来不是 ELT,而是 EL (T)
Curriculum Vitae;CV
IPFS部署及文件上传(golang)