当前位置:网站首页>作文以记之 ~ 二叉树的后序遍历
作文以记之 ~ 二叉树的后序遍历
2022-04-23 07:27:00 【小强~】
作文以记之 ~ 二叉树的后序遍历
0、前言
本篇博客是力扣上 145. 二叉树的后序遍历 题的题解,题目简单,写博客主要是因为它也可以作为DFS的例题~
二叉树的前序遍历可 点击此处 进行查看!
二叉树的中序遍历可 点击此处 进行查看!
1、题目描述
2、解题思路
2.1 方法1 ~ 递归
2.1.1 思路
二叉树的后序遍历是按照访问 左子树 – 右子树 – 根节点 的方式遍历目标树,而在访问左子树或者右子树的时候可按照同样的方式遍历,直到遍历完整棵树。结合这种方式可直接使用递归!
2.1.2 程序代码
/** * 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 postorder(TreeNode* root, vector<int>& ans)
{
if(!root)
return;
postorder(root->left,ans);
postorder(root->right,ans);
ans.push_back(root->val);
}
vector<int> postorderTraversal(TreeNode* root) {
vector<int> ans;
postorder(root,ans);
return ans;
}
};
2.2 方法2 ~ DFS
2.2.1 思路
这个思路呢,是基于力扣上所给 DFS 模板2 的内容编写的。虽然感觉更像是迭代,害!
而此处的思路相当于是将方法1的思路展开,从隐式栈转成了显式栈,其他都相同,具体实现可以看下面的代码。只不过此处展开时还需要对右子树进行判断!具体如下述程序
2.2.2 程序代码
/** * 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> ans;
stack<TreeNode*> st;
TreeNode* cur = root;
TreeNode* prev;
while(cur || !st.empty())
{
while(cur)
{
st.push(cur);
cur = cur->left;
}
TreeNode* top = st.top();
if(top->right == nullptr || top->right == prev)
{
ans.push_back(top->val);
st.pop();
prev = top;
}
else
cur = top->right;
}
return ans;
}
};
版权声明
本文为[小强~]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_51961114/article/details/124353170
边栏推荐
- One click cleanup of pycharm and jupyter cache files under the project
- [Effective Go 中文翻译] 第一篇
- 【解释】get ORA-12838: cannot read/modify an object after modifying it in parallel
- 每周leetcode - 06 数组专题 7~739~50~offer 62~26~189~9
- Weekly leetcode - 06 array topics 7 ~ 739 ~ 50 ~ offer 62 ~ 26 ~ 189 ~ 9
- Somme numérique de la chaîne de calcul pour un problème simple de leetcode
- Qt读写XML文件
- 关于ORB——SLAM运行中关键帧位置越来越近的异常说明
- The whole house intelligence bet by the giant is driving the "self revolution" of Hisense, Huawei and Xiaomi
- QFileDialog 选择多个文件或文件夹
猜你喜欢
LeetCode-199-二叉树的右视图
2022.4.11-4.17 AI行业周刊(第93期):AI行业的困局
Detailed explanation of ansible automatic operation and maintenance (I) installation and deployment, parameter use, list management, configuration file parameters and user level ansible operating envi
mysql查询字符串类型的字段使用数字类型查询时问题
Smart business card applet business card details page function implementation key code
Somme numérique de la chaîne de calcul pour un problème simple de leetcode
Campus transfer second-hand market source code download
WordPress爱导航主题 1.1.3 简约大气网站导航源码网址导航源码
一个必看的微信小程序开发指南1-基础知识了解
监控智能回放是什么,如何使用智能回放查询录像
随机推荐
Data security has become a hidden danger. Let's see how vivo can make "user data" armor again
Find the largest of 3 strings (no more than 20 characters per string).
The third divisor of leetcode simple question
室内定位技术对比
二维01背包
How to import Excel data in SQL server, 2019 Edition
AAAI 2022 recruit speakers!!
WordPress love navigation theme 1.1.3 simple atmosphere website navigation source code website navigation source code
利用Js实现一个千分位
The whole house intelligence bet by the giant is driving the "self revolution" of Hisense, Huawei and Xiaomi
MySQL数据库中delete、truncate、drop原理详解
使用JWT生成与解析Token
LeetCode简单题之三除数
Data deletion and modification (MySQL)
青苹果影视系统源码 影视聚合 影视导航 影视点播网站源码
搜一下导航完整程序源码
LeetCode简单题之计算字符串的数字和
社区团购小程序源码+界面diy+附近团长+供应商+拼团+菜谱+秒杀+预售+配送+直播
Goland 调试go使用-大白记录
线程的调度(优先级)