当前位置:网站首页>二叉树的遍历 递归+迭代
二叉树的遍历 递归+迭代
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;
}
};
访问当前结点,如果不为空,就访问其左结点,如果为空但此时栈不为空,就访问栈顶元素,然后访问其右结点。
边栏推荐
- 剑指Offer(二十):包含min函数的栈
- 13 managing resources by objects
- 在altium designer中禁用USBJATG
- 剑指Offer(九):变态跳台阶
- RT-Thread 学习笔记(七)---开启基于SPI Flash的elmfat文件系统(中)
- C language callback function
- MySQL速学-2021-09-01
- The problem of formatting IAR sprintf floating point to 0.0 in UCOS assembly
- 关于硕博士开题报告编写的思考
- .net operation redis set unordered collection
猜你喜欢

2021-08-12函数递归_和鹏哥学习C语言

RT thread learning notes (VII) -- open the elmfat file system based on SPI flash (middle)

mysql20210906

Oracle cannot start tnslistener service cannot start
![[leetcode daily question 2021/2/13]448. Find all the missing numbers in the array](/img/9b/624416fa6a408bf64ca5438273176b.png)
[leetcode daily question 2021/2/13]448. Find all the missing numbers in the array
![[machine learning notes] [face recognition] deeplearning ai course4 4th week programming](/img/7e/9c0e88097b90c0c24ebf86f090805b.png)
[machine learning notes] [face recognition] deeplearning ai course4 4th week programming

RT-Thread 学习笔记(五)---编辑、下载、调试程序

工厂模式详解

Flutter编译报错 version of NDK matched the requested version 21.0.6113669. Versions available locally: 2

Disable usbjatg in Altium Designer
随机推荐
GIS方法类期刊和论文的综述(Introduction)怎么写?
Redis implementation of distributed lock solution
MD5 encryption
MFC图片控件
11 handle "self assignment" in operator=
C语言鹏哥20210812C语言函数
RT thread learning notes (I) -- configure RT thread development environment
使用Geoprocessor 工具
RT thread learning notes (VIII) -- start the elmfat file system based on SPI flash (Part 2)
按二进制数中1的个数分类
Issue 6: which mainstream programming language should college students choose
Pengge C language lesson 4 (3)
STM32 Alibaba cloud mqtt esp8266 at command
.net operation redis string string
RT-Thread 学习笔记(六)--- 开启基于SPI Flash的elmfat文件系统(上)
RT thread learning notes (III) -- building a compilation environment with scons
控制随机抽中几率 [ C# | Random ]
多目标优化系列1---NSGA2的非支配排序函数的讲解
RT-Thread 学习笔记(七)---开启基于SPI Flash的elmfat文件系统(中)
Analysis of the transaction problem of chained method call