当前位置:网站首页>作文以记之 ~ 二叉树的前序遍历
作文以记之 ~ 二叉树的前序遍历
2022-04-23 07:27:00 【小强~】
作文以记之 ~ 二叉树的前序遍历
0、前言
本篇博客是力扣上 144. 二叉树的前序遍历 题的题解,题目简单,写博客主要是因为它也可以作为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 preorder(TreeNode* root,vector<int>& ans)
{
if(!root)
return;
ans.push_back(root->val);
preorder(root->left,ans);
preorder(root->right,ans);
}
vector<int> preorderTraversal(TreeNode* root) {
vector<int> ans;
preorder(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> preorderTraversal(TreeNode* root) {
vector<int> ans;
TreeNode* cur = root;
stack<TreeNode*> st;
while(cur || !st.empty())
{
while(cur)
{
st.push(cur);
ans.push_back(cur->val);
cur = cur->left;
}
cur = st.top();
st.pop();
cur=cur->right;
}
return ans;
}
};
版权声明
本文为[小强~]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_51961114/article/details/124352816
边栏推荐
- Ubuntu安装Mysql并查询平均成绩
- An example of network communication based on TCP / IP protocol -- file transmission
- Goland 调试go使用-大白记录
- The following program deletes n consecutive words starting from the ith character from the string str
- Data security has become a hidden danger. Let's see how vivo can make "user data" armor again
- 校园转转二手市场源码下载
- dmp引擎工作总结(2021,光剑)
- Sword finger offer Day24 math (medium)
- Online yaml to XML tool
- How to import Excel data in SQL server, 2019 Edition
猜你喜欢
2022.4.11-4.17 AI行业周刊(第93期):AI行业的困局
LeetCode简单题之统计字符串中的元音子字符串
AQS & ReentrantLock 实现原理
Idea: export Yapi interface using easyyapi plug-in
关于ORB——SLAM运行中关键帧位置越来越近的异常说明
How to read books and papers
stm32以及freertos 堆栈解析
Briefly describe the hierarchical strategy of memory
分布式消息中间件框架选型-数字化架构设计(7)
Qtablewidget header customization and beautification developed by pyqt5 (with source code download)
随机推荐
跨域配置报错: When allowCredentials is true, allowedOrigins cannot contain the special value “*“
一篇文章看懂变量提升(hoisting)
PHP generates short links: convert numbers to letters and letters to numbers
万物互联下如何对设备进行加密
Samsung, March to the west again
Online app resource download website source code
Compiler des questions de principe - avec des réponses
5.6 综合案例-RTU-
浅谈ES6尾调优化
Manipulator motion planning in 3C assembly
C outputs a two-dimensional array with the following characteristics.
数论求a^b(a,b为1e12级别)的因子之和
LeetCode-199-二叉树的右视图
Qt编译QtXlsx库
干货!以点为形:可微分的泊松求解器
Campus transfer second-hand market source code download
Rotation function of leetcode medium problem
PyQt5开发之QTableWidget表头自定义与美化(附源代码下载)
Planification du mouvement du manipulateur dans l'assemblage 3c
谈谈那些基础但不简单的股票数据