当前位置:网站首页>【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;
}
};
边栏推荐
- BioVendor人俱乐部细胞蛋白(CC16)Elisa试剂盒研究领域
- svm.SVC应用实践1--乳腺癌检测
- EasyGBS平台播放视频时偶尔出现播放失败是什么原因?
- [ORB_SLAM2] SetPose, UpdatePoseMatrices
- 2022-07-30 mysql8 executes slow SQL-Q17 analysis
- Unable to log in to the Westward Journey
- C#测试项目中属性的用法
- Moonbeam and Project integration of the Galaxy, bring brand-new user experience for the community
- 四元数、罗德里格斯公式、欧拉角、旋转矩阵推导和资料
- 周鸿祎称微软抄袭,窃取360安全模式
猜你喜欢
ofstream,ifstream,fstream read and write files
Nanoprobes多组氨酸 (His-) 标签标记:重组蛋白检测方案
qt点云配准软件
Nanoprobes丨1-巯基-(三甘醇)甲醚功能化金纳米颗粒
搭建zabbix监控及邮件报警(超详细教学)
Electronic Manufacturing Warehouse Barcode Management System Solution
第11章_数据库的设计规范
Chopper webshell feature analysis
analog IC layout-Parasitic effects
AWR analysis report questions for help: How can SQL be optimized from what aspects?
随机推荐
OC中new和init的区别
最大层内元素和
永磁同步电机36问(三)——SVPWM代码实现
ofstream,ifstream,fstream read and write files
Chapter 7 Noise analysis
analog IC layout-Parasitic effects
Install mysql using docker
Nacos源码分析专题(二)-服务注册
指针数组和数组指针
NIO's Sword
第10章_索引优化与查询优化
使用DBeaver进行mysql数据备份与恢复
AI target segmentation capability for fast video cutout without green screen
yaml
欧拉公式的证明
线程的不同状态
四元数、罗德里格斯公式、欧拉角、旋转矩阵推导和资料
Docker-compose安装mysql
Entry name 'org/apache/commons/codec/language/bm/gen_approx_greeklatin.txt' collided
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?