当前位置:网站首页>作文以记之 ~ 二叉树的后序遍历
作文以记之 ~ 二叉树的后序遍历
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
边栏推荐
- Draw a circle quickly in MATLAB (the one that can be drawn directly given the coordinates and radius of the center of the circle)
- The following program deletes n consecutive words starting from the ith character from the string str
- MySQL数据库中delete、truncate、drop原理详解
- 获取TrustedInstaller权限
- Listed on the Shenzhen Stock Exchange: the market value is 5.2 billion yuan. Lu is the East and his daughter is American
- JS converts tree structure data into one-dimensional array data
- Goland 调试go使用-大白记录
- A simple theme of Typecho with beautiful appearance_ Scarfskin source code download
- [learning] audio and video development from scratch (9) -- nuplayer
- Transformer-XL: Attentive Language ModelsBeyond a Fixed-Length Context 论文总结
猜你喜欢
一个必看的微信小程序开发指南1-基础知识了解
Qt编译QtXlsx库
vslam PPT
Green apple film and television system source code film and television aggregation film and television navigation film and television on demand website source code
Vowel substring in statistical string of leetcode simple problem
396. Rotate Function
每周leetcode - 06 数组专题 7~739~50~offer 62~26~189~9
PyQt5开发之QTableWidget表头自定义与美化(附源代码下载)
Discussion on ES6 tail tune optimization
校园转转二手市场源码下载
随机推荐
扎心了!一女子发朋友圈羡慕别人按时发工资被开除,连点赞的同事也一同被开除了...
Comparison of indoor positioning technology
An example of network communication based on TCP / IP protocol -- file transmission
Goland 调试go使用-大白记录
Qt利用QtXlsx操作excel文件
一个必看的微信小程序开发指南1-基础知识了解
情境领导者-第七章、解决绩效问题
有意思的js 代码
室内定位技术对比
多目视觉SLAM
Community group purchase applet source code + interface DIY + nearby leader + supplier + group collage + recipe + second kill + pre-sale + distribution + live broadcast
vslam PPT
Generate and parse tokens using JWT
数据的删除和修改操作(mysql)
输入/输出系统
2022.4.11-4.17 AI industry weekly (issue 93): the dilemma of AI industry
搜一下导航完整程序源码
C outputs a two-dimensional array with the following characteristics.
Online app resource download website source code
[effective go Chinese translation] function