当前位置:网站首页>【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;
}
};
边栏推荐
- Power button 1374. Generate each character string is an odd number
- 字符串常用方法
- 工程师如何对待开源
- 【每日一道LeetCode】——9. 回文数
- Reflex WMS Intermediate Series 7: What should I do if I want to cancel the picking of an HD that has finished picking but has not yet been loaded?
- OC中成员变量,实例变量和属性之间的区别和联系
- Pinduoduo leverages the consumer expo to promote the upgrading of domestic agricultural products brands and keep pace with international high-quality agricultural products
- 最大层内元素和
- 网络层解析——IP协议、地址管理、路由选择
- Remember a pit for gorm initialization
猜你喜欢

记一个gorm初始化的坑

指针数组和数组指针

因为WiFi原因navicat 无法连接数据库Mysql

Nanoprobes丨1-巯基-(三甘醇)甲醚功能化金纳米颗粒

How engineers treat open source

BI - SQL 丨 WHILE

Install mysql using docker

The failure to create a role in Dahua Westward Journey has been solved

2022-08-01 mysql/stoonedb慢SQL-Q18分析

Nanoprobes丨1-mercapto-(triethylene glycol) methyl ether functionalized gold nanoparticles
随机推荐
Electronic Manufacturing Warehouse Barcode Management System Solution
【CNN记录】tensorflow slice和strided_slice
Install mysql using docker
Nanoprobes多组氨酸 (His-) 标签标记:重组蛋白检测方案
1688以图搜货
BioVendor Human Club Cellular Protein (CC16) Elisa Kit Research Fields
记一次gorm事务及调试解决mysql死锁
Reflex WMS Intermediate Series 7: What should I do if I want to cancel the picking of an HD that has finished picking but has not yet been loaded?
记一个gorm初始化的坑
一次SQL优化,数据库查询速度提升 60 倍
Unable to log in to the Westward Journey
机器人领域期刊会议汇总
局部敏感哈希:如何在常数时间内搜索Embedding最近邻
isa指针使用详情
Safety (1)
NIO's Sword
analog IC layout-Environmental noise
【 wheeled odometer 】
JVM调优实战
A good book for newcomers to the workplace