当前位置:网站首页>二叉树的遍历 递归+迭代
二叉树的遍历 递归+迭代
2022-07-26 10:42:00 【Forest_1010】
递归法
前序遍历
/** * 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 traversal(TreeNode* cur,vector<int>&vec)//传引用
{
if(cur==NULL) return;
vec.push_back(cur->val);//根
traversal(cur->left,vec);//左
traversal(cur->right,vec);//右
}
vector<int> postorderTraversal(TreeNode* root) {
vector<int>result;
traversal(root,result);
return result;
}
};
中序遍历和后序遍历的实现也很简单,只要把前序遍历中的顺序调换一下即可。
迭代法
递归中是隐藏了一个栈,在迭代法中需要将栈显式的表现出来。
前序遍历
/** * 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> result;
stack<TreeNode*> st;
st.push(root);
while(!st.empty())
{
TreeNode* cur=st.top();//访问栈顶元素
st.pop();//弹出栈顶元素
if(cur!=NULL) //当栈顶元素不为空时(没访问到叶子结点时)
result.push_back(cur->val);
else //若栈顶元素为空(遇到叶子结点)
continue;
st.push(cur->right);//右子树结点先入栈
st.push(cur->left);
}
return result;
}
};
后序遍历
后序遍历只要在前序遍历的基础上稍做改动就好。
前序遍历是:根、左、右,最终的后序遍历是:左、右、根。
思路:在前序遍历中,改变为根、右、左,然后在反转一下vector中的顺序即可。
中序遍历
中序遍历就和其它两种不一样了,入栈顺序不一致,先上代码
/** * 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> result;
stack<TreeNode*> st;
TreeNode* cur=root;
while(cur!=NULL || !st.empty())
{
if(cur!=NULL)
{
st.push(cur);
cur=cur->left;
}
else
{
cur=st.top();
st.pop();
result.push_back(cur->val);
cur=cur->right;
}
}
return result;
}
};
访问当前结点,如果不为空,就访问其左结点,如果为空但此时栈不为空,就访问栈顶元素,然后访问其右结点。
边栏推荐
- Write to esp8266 burning brush firmware
- .net5wtm (asp.net core) PgSQL unpacking operation
- [dectectron2] follow the official demo
- Datav beautiful data screen production experience
- $router和$route的区别
- 多目标优化系列1---NSGA2的非支配排序函数的讲解
- RT-Thread 学习笔记(八)---开启基于SPI Flash的elmfat文件系统(下)
- Phase 4: one of College Students' vocational skills preparation in advance
- flutter dart生成N个区间范围内不重复随机数List
- 按二进制数中1的个数分类
猜你喜欢
Issue 5: the second essential skill for College Students
第8期:云原生—— 大学生职场小白该如何学
The problem of large fluctuation of hx711 data
sigmod 函数与softmax 函数对比
[leetcode daily question 2021/4/23]368. Maximum divisible subset
[leetcode daily question 2021/8/31] 1109. Flight reservation statistics [medium] differential array
Anaconda is used on vscode (the environment has been configured)
RT thread learning notes (VII) -- open the elmfat file system based on SPI flash (middle)
抽象工厂及其改进示例
【机器学习小记】【人脸识别】deeplearning.ai course4 4th week programming
随机推荐
$router和$route的区别
Error[pe147]: declaration is incompatible with 'error problem
27.移除元素
.net operation redis list list
Sql Server之查询总结
C language calculation date interval days
Problems encountered in QRcode QR code (C language)
winpcap 抓包函数pcap_loop(),停止问题
MySQL速学-2021-09-01
点击el-dropdown-item/@click.native
12 don't forget every component when copying an object
Happens-Before原则深入解读
按二进制数中1的个数分类
使用定位实现左中右布局,中间内容自适应
解决:无法加载文件 C:\Users\user\AppData\Roaming\npm\npx.ps1,因为在此系统上禁止运行脚本 。
router.push(),router.repalce(),router.go()使用
剑指Offer(二十):包含min函数的栈
Anaconda is used on vscode (the environment has been configured)
鹏哥C语言第七节课总结
鹏哥C语言20210811程序结构作业