当前位置:网站首页>二叉树的遍历 递归+迭代
二叉树的遍历 递归+迭代
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;
}
};
访问当前结点,如果不为空,就访问其左结点,如果为空但此时栈不为空,就访问栈顶元素,然后访问其右结点。
边栏推荐
猜你喜欢

RT thread learning notes (VI) -- start the elmfat file system based on SPI flash (Part 1)

Issue 6: which mainstream programming language should college students choose

344.反转字符串

RT-Thread 学习笔记(七)---开启基于SPI Flash的elmfat文件系统(中)
![[leetcode daily question 2021/2/14]765. Lovers hold hands](/img/be/8639a05c733638bf0b3fdeb11abccf.png)
[leetcode daily question 2021/2/14]765. Lovers hold hands
![[leetcode daily question 2021/8/30]528. Choose randomly by weight [medium]](/img/13/c6cb176d7065035f60d55ad20ed1bf.png)
[leetcode daily question 2021/8/30]528. Choose randomly by weight [medium]

反射机制简述

Phase 4: one of College Students' vocational skills preparation in advance
![[paper after dinner] deep mining external perfect data for chestx ray disease screening](/img/d6/41c75d292c26b2e7e116767a51eb5e.png)
[paper after dinner] deep mining external perfect data for chestx ray disease screening

第8期:云原生—— 大学生职场小白该如何学
随机推荐
Zongzi battle - guess who can win
Flutter 防止科学计数并去除尾数无效0
访问权限——private,public,protected
toolstrip 去边框
Oracle创建索引
Phase 4: one of College Students' vocational skills preparation in advance
鹏哥C语言第七节课总结
剑指Offer(二十):包含min函数的栈
Issue 8: cloud native -- how should college students learn in the workplace
Happens-Before原则深入解读
MySQL速学-2021-09-01
使用Geoprocessor 工具
2021-08-12函数递归_和鹏哥学习C语言
RT thread learning notes (I) -- configure RT thread development environment
剑指Offer(二十):包含min函数的栈
多目标优化系列1---NSGA2的非支配排序函数的讲解
7-25 0-1背包 (50分)
按二进制数中1的个数分类
Mlx90640 infrared thermal imager temperature sensor module development notes (VI) pseudo color coding of infrared images
Successfully transplanted stemwin v5.22 on Shenzhou IV development board