当前位置:网站首页>作文以记之 ~ 二叉树的前序遍历
作文以记之 ~ 二叉树的前序遍历
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
边栏推荐
- Samsung, March to the west again
- Multi vision slam
- Qtablewidget header customization and beautification developed by pyqt5 (with source code download)
- Install MySQL for Ubuntu and query the average score
- 剑指offer day24 数学(中等)
- 有意思的js 代码
- Smart business card applet business card details page function implementation key code
- 青苹果影视系统源码 影视聚合 影视导航 影视点播网站源码
- LeetCode-199-二叉树的右视图
- A simple theme of Typecho with beautiful appearance_ Scarfskin source code download
猜你喜欢
关于ORB——SLAM运行中关键帧位置越来越近的异常说明
一个必看的微信小程序开发指南1-基础知识了解
Green apple film and television system source code film and television aggregation film and television navigation film and television on demand website source code
Kubernetes in browser and IDE | interactive learning platform killercoda
vslam PPT
nn.Module类的讲解
扎心了!一女子发朋友圈羡慕别人按时发工资被开除,连点赞的同事也一同被开除了...
One click cleanup of pycharm and jupyter cache files under the project
Rotation function of leetcode medium problem
利用Js实现一个千分位
随机推荐
1216_ MISRA_ C standard learning notes_ Rule requirements for control flow
396. Rotate Function
stm32以及freertos 堆栈解析
获取TrustedInstaller权限
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
编译原理题-带答案
nn.Module类的讲解
Comparison of indoor positioning methods of several intelligent robots
[appium] encountered the problem of switching the H5 page embedded in the mobile phone during the test
[Effective Go 中文翻译] 第一篇
二维01背包
The following program deletes n consecutive words starting from the ith character from the string str
Brief description of CPU
Online yaml to XML tool
A simple theme of Typecho with beautiful appearance_ Scarfskin source code download
校园转转二手市场源码下载
synchronized 实现原理
岛屿的个数
The third divisor of leetcode simple question
colorui 解决底部导航遮挡内容问题