当前位置:网站首页>二叉树前,中,后序遍历
二叉树前,中,后序遍历
2022-07-02 12:12:00 【-小小白-】
-前言
树是一种常用的数据结构,用来模拟具有树状结构性质的数据集合。树里的每一个节点(结构体)有一个值和一个包含所有子节点的列表。二叉树则是更典型的树结构,每个结点最多有两个子节点。接下来记录二叉树的三种遍历方法。
-中序遍历
什么是二叉树的中序遍历:按照访问左子树——根节点——右子树的方式遍历这棵树,而在访问左子树或者右子树的时候,我们按照同样的方式遍历,直到遍历完整棵树。这里先讲中序排序,是因为中序排序与后序排序的迭代法思路较为类似,相对好理解一点,而前序遍历比较不同,放在最后说。

- 以上图这个树来说,中序遍历的结果应该是DBEAFCG-
递归法
树的遍历思路显然可以用递归来实现。递归的出口为结点空,先记录当前节点的数据,再先遍历root->left后遍历root->right即可。递归较容易理解
void inorder(struct TreeNode* root, int* res, int* returnSize){
if(root == NULL){
return;
}
inorder(root->left, res, returnSize);
res[(*returnSize)++] = root->val;
inorder(root->right, res, returnSize);
}
int* inorderTraversal(struct TreeNode* root, int* returnSize){
int* res = (int*)malloc(sizeof(int) * 2000);
*returnSize = 0;
inorder(root, res, returnSize);
return res;
}迭代法
迭代法与递归的原理很相似,都是用了栈的思想(入栈出栈),递归的栈是暗含的,迭代就是直接使用一个栈帮助遍历。迭代法个人感觉较难理解,学习的时候最好能自己动手在纸上模拟一下过程。
int* inorderTraversal(struct TreeNode* root, int* returnSize){
int* res = (int*)malloc(sizeof(int) * 2000);
*returnSize = 0;
if(root == NULL){
return res;
}
struct TreeNode** Node = (struct TreeNode**)malloc(sizeof(struct TreeNode*) * 501);
//struct TreeNode* Node[500];
int top;
while(root != NULL || top != 0){
while(root != NULL){
Node[top++] = root;
root = root->left;
}
root = Node[--top];
res[(*returnSize)++] = root->val;
root = root->right;
}
return res;
}
-后序遍历
什么是二叉树的后序遍历:按照访问左子树——右子树——根节点的方式遍历这棵树。

- 以上图这个树来说,后序遍历的结果应该是DEBFGCA-
递归法
递归法大部分代码与中序相同,唯一不同的在于递归函数里记录数据的先后顺序
postorder(root->left, res, returnSize);
postorder(root->right, res, returnSize);
res[(*returnSize)++] = root->val;迭代法
后序遍历的迭代法与中序的迭代法思路大致相同,不同在于出栈之后要考虑右子树是否为空,不为空的话需要把出栈的数值再入栈,不记录数据,且从右子树开始继续遍历直到遇到右子树为空停止。设置prev结点的用途是在开始记录右子树的数据时标记记录过的结点,帮助后续出栈的进行。
int* postorderTraversal(struct TreeNode* root, int* returnSize){
int* res = (int*)malloc(sizeof(int) * 2000);
*returnSize = 0;
if(root == NULL){
return res;
}
struct TreeNode* Node[500];
struct TreeNode* prev = NULL;
int top = 0;
while(root != NULL || top != 0){
while(root != NULL){
Node[top++] = root;
root = root->left;
}
root = Node[--top];
if(root->right == NULL || root->right == prev){
res[(*returnSize)++] = root->val;
prev = root;
root = NULL;
}
else{
Node[top++] = root;
root = root->right;
}
}
return res;
}-前序遍历
什么是二叉树的前序遍历:按照访问根节点——左子树——右子树的方式遍历这棵树。

- 以上图这个树来说,前序遍历的结果应该是ABDECFG-
递归法
代码大部分与中序相同,不同之处在于:
res[(*returnSize)++] = root->val;
preorder(root->left, res, returnSize);
preorder(root->right, res, returnSize);迭代法
前序排序的迭代法不同在于入栈时就存储数据,具体代码如下。
int* preorderTraversal(struct TreeNode* root, int* returnSize){
int* res = (int*)malloc(sizeof(int) * 2000);
*returnSize = 0;
if(root == NULL){
return res;
}
struct TreeNode* Node[500];
int top = 0;
while(root != NULL || top != 0){
while(root != NULL){
res[(*returnSize)++] = root->val;
Node[top++] = root;
root = root->left;
}
root = Node[--top];
root = root->right;
}
retuen res;
}边栏推荐
- Leetcode skimming -- count the number of numbers with different numbers 357 medium
- Facing the challenge of "lack of core", how can Feiling provide a stable and strong guarantee for customers' production capacity?
- Oracle primary key auto increment
- Two traversal sequences are known to construct binary trees
- LeetCode刷题——奇偶链表#328#Medium
- 【LeetCode】876-链表的中间结点
- Real estate market trend outlook in 2022
- Leetcode skimming -- sum of two integers 371 medium
- 2022 年辽宁省大学生数学建模A、B、C题(相关论文及模型程序代码网盘下载)
- Deux séquences ergodiques connues pour construire des arbres binaires
猜你喜欢

密码学基础知识

Loss function and positive and negative sample allocation: Yolo series

百变大7座,五菱佳辰产品力出众,人性化大空间,关键价格真香

Yolo format data set processing (XML to txt)

微信支付宝账户体系和支付接口业务流程

. Net again! Happy 20th birthday

How to find a sense of career direction
![[leetcode] 1162 map analysis](/img/9a/d04bde0417d4d5232950a4e260eb91.png)
[leetcode] 1162 map analysis

党史纪实主题公益数字文创产品正式上线

Be a good gatekeeper on the road of anti epidemic -- infrared thermal imaging temperature detection system based on rk3568
随机推荐
Custom exception
SQL stored procedure
NBA player analysis
Build your own semantic segmentation platform deeplabv3+
15_ Redis_ Redis. Conf detailed explanation
Equipped with Ti am62x processor, Feiling fet6254-c core board is launched!
【LeetCode】877-石子游戏
彻底弄懂浏览器强缓存和协商缓存
LeetCode刷题——统计各位数字都不同的数字个数#357#Medium
2278. 字母在字符串中的百分比
LeetCode_ String_ Simple_ 412.Fizz Buzz
【LeetCode】977-有序数组的平方
6095. 强密码检验器 II
02.面向容器化后,必须面对golang
How to intercept the value of a key from the JSON string returned by wechat?
Solve the problem of frequent interruption of mobaxterm remote connection
LeetCode刷题——两整数之和#371#Medium
04.进入云原生后的企业级应用构建的一些思考
FPGA - clock-03-clock management module (CMT) of internal structure of 7 Series FPGA
[leetcode] 486 predict winners