当前位置:网站首页>二叉树的遍历 递归+迭代
二叉树的遍历 递归+迭代
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;
}
};
访问当前结点,如果不为空,就访问其左结点,如果为空但此时栈不为空,就访问栈顶元素,然后访问其右结点。
边栏推荐
- 按二进制数中1的个数分类
- .net operation redis string string
- 扫雷pro版2021-08-19
- [leetcode daily question 2021/2/13]448. Find all the missing numbers in the array
- 20210807#1 C语言程序结构
- 10 let operator= return a reference to *this
- putty的使用教程
- [转]ArcGIS中判断两个Geometry之间的关系
- Redis docker instance and data structure
- C language calculation date interval days
猜你喜欢

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

文案秘籍七步曲至----文献团队协作管理

在神州IV开发板上为STemWin 5.22加入触屏驱动

从蚂蚁的觅食过程看团队研发(转载)

RT-Thread 学习笔记(一)---配置RT-Thread开发环境

Zongzi battle - guess who can win

一文详解Nodejs中fs文件模块与path路径模块

按二进制数中1的个数分类

Centos8 (liunx) deploying WTM (asp.net 5) using PgSQL

Anaconda is used on vscode (the environment has been configured)
随机推荐
Redis docker instance and data structure
router.push(),router.repalce(),router.go()使用
Koin
点击el-dropdown-item/@click.native
Redis implementation of distributed lock solution
构建ARM嵌入式开发环境
$router和$route的区别
Error[pe147]: declaration is incompatible with 'error problem
工厂模式详解
回到顶部的几种方案(js)
2021-08-14三子棋
C语言鹏哥20210812C语言函数
Sql Server之查询总结
[leetcode daily question 2021/5/8]1723. The shortest time to complete all work
使用flex实现左中右布局,中间自适应
JS对象赋值问题
.net operation redis list list
关于硕博士开题报告编写的思考
sigmod 函数与softmax 函数对比
SAP ABAP 守护进程的实现方式