当前位置:网站首页>二叉树的结点定义 、遍历 ~
二叉树的结点定义 、遍历 ~
2022-07-22 23:23:00 【柯基@】
- 结点定义
typedef struct BTNode{
char data; //data 的类型因题目要求而异
struct BTNode *lchild;
struct BTNode *rchild;
}BTNode;
遍历
- 先序遍历
void preorder(BTNode *p){
if(p!=NULL){
visit(p);
preorder(p->lchild);
preorder(p->rchild);
}
}
- 中序遍历
void inorder(BTNode *p){
if(p!=NULL){
inorder(p->lchild);
visit(p);
inorder(p->rchild);
}
}
- 后序遍历
void postorder(BTNode *p){
if(p!=NULL){
postorder(p->lchild);
postorder(p->rchild);
visit(p);
}
}
- 层次遍历
/* 思路: 1.创建一个空队列 2.判断二叉树是否为空,不为空则根节点入队 3.循环直到队列为空 { 头结点出队并访问,出队头结点的左右结点入队 } */
void levelorder(BTNode *p){
//创建一个空队列
BTNode *queue[maxmize];
int front=0,rear=0;
BTNode *q;
//判断二叉树是否为空,不为空则根节点入队
if(p!=NULL){
queue[rear]=p;
rear=(raer+1)%MaxSize;
//循环直到队列为空 { 头结点出队并访问,出队头结点的左右结点入队 }
while(front!=rear){
q=queue[front]; //头结点出队并访问
front=(front+1)%MaxSize;
Visit(q);
if(q->lchild!=NULL){
//出队头结点的左结点入队
queue[rear]=q->lchild;
rear=(rear+1)%MaxSize;
}
if(q->rchild!=NULL){
//出队头结点的右结点入队
queue[rear]=q->rchild;
rear=(rear+1)%MaxSize;
}
}
}
}
注:层次遍历相对于前三种遍历而言稍微又一点点难度,还没理解的可以去抖音上看
4i:/ zoQ复制打开抖音,看看【有个花园的作品】Leetcode大神带你手把手搞定二叉树层序遍历 ЭRIb2Ucyf00dau8ÑÑ
边栏推荐
- 类和对象上案例
- Cases on classes and objects
- 关于常见排序的稳定性
- 构造函数的初始化、清理及const修饰成员函数
- 小迪和小辉
- promise(一)
- What if Alibaba cloud international forgets its member name or login password?
- Xiaohongshu joins hands with HMS core to enjoy HD vision and grow grass for a better life
- 【OPENVX】对象基本使用之vx_reference
- Web3流量聚合平台Starfish OS,给玩家元宇宙新范式体验
猜你喜欢

Promise (II)
[email protected], "/>Shell variables, system predefined variables $home, $pwd, $shell, $user, custom variables, special variables $n, $, $*, [email protected],

What is NFT? You don't know yet!
![[arxiv2022] grouptransnet: Group transformer Network for RGB - D Salient Object Detection](/img/be/534f92578f0d825f9b565c1785af18.png)
[arxiv2022] grouptransnet: Group transformer Network for RGB - D Salient Object Detection

Redistemplate pipeline use

我们来浅谈代码语言的魅力

Bryntum Kanban task board 5.1.0 JS Kanban

工控人,你真的了解你的五险一金吗?

Redis 配置文件

PKS的秘书&兄弟 | 温故知新
随机推荐
程序员可能还是程序员,码农可能只能是码农了
Typora set the title to automatically add sequence number
What type of die cutting machine can be used for paper?
Easily take you to the gate of turtle drawing
浅谈——网络安全架构设计(二)
气不过“自愿降薪”,裸辞面字节,四天三面,结局居然这样?
你必须知道的十大漏洞之失效的访问控制
On the stability of common sorting
黑马程序员-接口测试-四天学习接口测试-第二天-接口用例设计,测试点,功能测试,安全测试,性能测试,单接口测试,业务场景测试用例,postman简介,安装
【arXiv2022】GroupTransNet: Group Transformer Network for RGB-D Salient Object Detection
小红书携手HMS Core,畅玩高清视界,种草美好生活
Xmodem、Ymodem和Zmodem协议是最常用的三种通信协议
svn: E000022: Can‘t convert string from ‘UTF-8‘ to native encoding 问题解决
你知道怎么做好接口测试?
Container monitoring three swordsman cadvisor collects monitoring data + influxdb stores data + granfana shows an introduction to chart data
笔试强训第21天
Dark horse programmer - interface testing - four-day learning interface testing - the second day - Interface use case design, test points, function testing, security testing, performance testing, sing
又发福利!日历小程序源码
MySQL uses SQL statements to query all data with a field value divided by 10 equal to 0
promise(一)