当前位置:网站首页>Nine degree 1201 - traversal of binary sort number - binary sort tree "suggestions collection"
Nine degree 1201 - traversal of binary sort number - binary sort tree "suggestions collection"
2022-07-07 21:24:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack .
This is an orthodox tree construction and traversal problem . At the beginning, I also wanted to use array construction to replace the past , But I can't find it , Just use the pointer honestly . If the binary sort tree and traversal method are not clearly defined . It's best to read the data structure book and review .
#include<stdio.h>
struct node{
node *l;
node *r;
int val;
node(int a):val(a),l(NULL),r(NULL){};
};
node *root;
int n;
void qian(node *p){
printf("%d ",p->val);
if(p->l!=NULL)qian(p->l);
if(p->r!=NULL)qian(p->r);
}
void zhong(node *p){
if(p->l!=NULL)zhong(p->l);
printf("%d ",p->val);
if(p->r!=NULL)zhong(p->r);
}
void hou(node *p){
if(p->l!=NULL)hou(p->l);
if(p->r!=NULL)hou(p->r);
printf("%d ",p->val);
}
int main(){
int val;
node *p;
while(~scanf("%d",&n)){
root=NULL;
for(int i=0;i<n;i++){
scanf("%d",&val);
if(i==0){
root=new node(val);
continue;
}
p=root;
while(1){
if(val==p->val)break;
else if(val<p->val){
if(p->l==NULL){
p->l=new node(val);
break;
}
else{
p=p->l;continue;
}
}
else if(val>p->val){
if(p->r==NULL){
p->r=new node(val);
break;
}
else{
p=p->r;continue;
}
}
}
}
qian(root);
printf("\n");
zhong(root);
printf("\n");
hou(root);
printf("\n");
}
return 0;
}Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/116527.html Link to the original text :https://javaforall.cn
边栏推荐
- Object-C programming tips timer "suggestions collection"
- Codeforces round 296 (Div. 2) A. playing with paper[easy to understand]
- Make this crmeb single merchant wechat mall system popular, so easy to use!
- Jetty:配置连接器[通俗易懂]
- 【OpenCV 例程200篇】223. 特征提取之多边形拟合(cv.approxPolyDP)
- A brief understanding of the in arc__ bridge、__ bridge_ Retained and__ bridge_ transfer
- 201215-03-19—cocos2dx内存管理–具体解释「建议收藏」
- Demon daddy A3 stage near normal speed speech flow initial contact
- Insufficient permissions
- Introduction to referer and referer policy
猜你喜欢
![[C language] advanced pointer --- do you really understand pointer?](/img/ee/79c0646d4f1bfda9543345b9da0f25.png)
[C language] advanced pointer --- do you really understand pointer?
SQL注入报错注入函数图文详解

恶魔奶爸 B3 少量泛读,完成两万词汇量+

ISO 26262 - considerations other than requirements based testing

使用高斯Redis实现二级索引

C语言 整型 和 浮点型 数据在内存中存储详解(内含原码反码补码,大小端存储等详解)

Small guide for rapid formation of manipulator (12): inverse kinematics analysis

神兵利器——敏感文件发现工具

目标:不排斥 yaml 语法。争取快速上手

Goal: do not exclude yaml syntax. Try to get started quickly
随机推荐
死锁的产生条件和预防处理[通俗易懂]
Codeforces round 275 (Div. 2) C – diverse permutation (construction) [easy to understand]
Implement secondary index with Gaussian redis
C语言 整型 和 浮点型 数据在内存中存储详解(内含原码反码补码,大小端存储等详解)
guava多线程,futurecallback线程调用不平均
SQL injection error report injection function graphic explanation
Codeforces 474 F. Ant colony
How can big state-owned banks break the anti fraud dilemma?
Unity3d 4.3.4f1执行项目
Small guide for rapid formation of manipulator (12): inverse kinematics analysis
DataTable数据转换为实体
UVA 12230 – crossing rivers (probability) "suggested collection"
刚开户的能买什么股票呢?炒股账户安全吗
MySQL约束之默认约束default与零填充约束zerofill
Implementation of mahout Pearson correlation
H3C s7000/s7500e/10500 series post stack BFD detection configuration method
反诈困境,国有大行如何破局?
Tensorflow2. How to run under x 1 Code of X
GridView defines its own time for typesetting "suggestions collection"
Is private equity legal in China? Is it safe?