当前位置:网站首页>作文以记之 ~ 二叉树的后序遍历
作文以记之 ~ 二叉树的后序遍历
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
边栏推荐
猜你喜欢

Listed on the Shenzhen Stock Exchange: the market value is 5.2 billion yuan. Lu is the East and his daughter is American

通过实现参数解析器HandlerMethodArgumentResolver接口来自定义注解

synchronized 实现原理
![[untitled]](/img/bb/213d95b60651dfeadb239a70507506.png)
[untitled]

为什么会存在1px问题?怎么解决?

Ansible Automation Operation and Maintenance details (ⅰ) Installation and Deployment, Parameter use, list Management, Profile Parameters and user level ansible operating environment Construction

浅谈ES6尾调优化

Comparison of indoor positioning methods of several intelligent robots

396. Rotate Function

校园转转二手市场源码下载
随机推荐
[untitled]
CSV column extract column extraction
My heart's broken! A woman's circle of friends envied others for paying wages on time and was fired. Even her colleagues who liked her were fired together
sql 使用过的查询语句
PyQt5开发之QTableWidget表头自定义与美化(附源代码下载)
Qt读取路径下所有文件或指定类型文件(含递归、判断是否为空、创建路径)
User manual of Chinese version of solidity ide Remix
浅谈ES6尾调优化
Kubernetes in browser and IDE | interactive learning platform killercoda
网赚APP资源下载类网站源码
常用正则表达式
利用Js实现一个千分位
QFileDialog select multiple files or folders
Goland 调试go使用-大白记录
【解释】get ORA-12838: cannot read/modify an object after modifying it in parallel
C语言学习记录——삼십팔 字符串函数使用和剖析(2)
如何保护开源项目免遭供应链攻击-安全设计(1)
输入/输出系统
刨析——浏览器如何工作
Generate and parse tokens using JWT