当前位置:网站首页>作文以记之 ~ 二叉树的前序遍历
作文以记之 ~ 二叉树的前序遍历
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
边栏推荐
- JS converts tree structure data into one-dimensional array data
- There are some problems when using numeric type to query string type fields in MySQL
- PHP high precision computing
- AAAI 2022 recruit speakers!!
- Common regular expressions
- Talk about the basic but not simple stock data
- 欧圣电气深交所上市:市值52亿 陆为东父女为美国籍
- Discussion on ES6 tail tune optimization
- LeetCode简单题之计算字符串的数字和
- 每周leetcode - 06 数组专题 7~739~50~offer 62~26~189~9
猜你喜欢

Comparison of indoor positioning methods of several intelligent robots

2022.4.11-4.17 AI industry weekly (issue 93): the dilemma of AI industry

5.6 comprehensive case - RTU-

Qtablewidget header customization and beautification developed by pyqt5 (with source code download)

Idea: export Yapi interface using easyyapi plug-in

AAAI 2022招募讲者啦!!

Rearranging log files for leetcode simple question
![[untitled]](/img/bb/213d95b60651dfeadb239a70507506.png)
[untitled]

freertos学习02-队列 stream buffer message buffer

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
随机推荐
LeetCode中等题之旋转函数
常用正则表达式
LeetCode-199-二叉树的右视图
Thinkphp6 + JWT realizes login verification
Positioning of high precision welding manipulator
clang 如何产生汇编文件
Compiling principle questions - with answers
[go] common concurrency model [generic version]
WordPress love navigation theme 1.1.3 simple atmosphere website navigation source code website navigation source code
Jetson Xavier NX (3) bazel mediapipe installation
【学习】从零开始的音视频开发(9)——NuPlayer
Common regular expressions
使用JWT生成与解析Token
校园转转二手市场源码下载
Rearranging log files for leetcode simple question
PHP high precision computing
WordPress爱导航主题 1.1.3 简约大气网站导航源码网址导航源码
Discussion on ES6 tail tune optimization
Manipulator motion planning in 3C assembly
微信小程序 catchtap=“toDetail“ 事件问题