当前位置:网站首页>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
边栏推荐
- Can Huatai Securities achieve Commission in case of any accident? Is it safe to open an account
- ISO 26262 - considerations other than requirements based testing
- UVA 11080 – place the guards
- Implementation of mahout Pearson correlation
- 恶魔奶爸 A1 语音听力初挑战
- sqlHelper的增删改查
- 恶魔奶爸 A0 英文零基础的自我提升路
- Écrivez une liste de sauts
- 201215-03-19 - cocos2dx memory management - specific explanation "recommended collection"
- How to meet the dual needs of security and confidentiality of medical devices?
猜你喜欢
Is embedded system really safe? [how does onespin comprehensively solve the IC integrity problem for the development team]
The latest version of codesonar has improved functional security and supports Misra, c++ parsing and visualization
C语言多角度帮助你深入理解指针(1. 字符指针2. 数组指针和 指针数组 、数组传参和指针传参3. 函数指针4. 函数指针数组5. 指向函数指针数组的指针6. 回调函数)
Details of C language integer and floating-point data storage in memory (including details of original code, inverse code, complement, size end storage, etc.)
【OpenCV 例程200篇】223. 特征提取之多边形拟合(cv.approxPolyDP)
Codesonar Webinar
Goal: do not exclude yaml syntax. Try to get started quickly
The new version of onespin 360 DV has been released, refreshing the experience of FPGA formal verification function
[paper reading] maps: Multi-Agent Reinforcement Learning Based Portfolio Management System
How does codesonar help UAVs find software defects?
随机推荐
[function recursion] do you know all five classic examples of simple recursion?
A brief understanding of the in arc__ bridge、__ bridge_ Retained and__ bridge_ transfer
FatMouse&#39; Trade (Hangdian 1009)
Mahout-Pearson correlation的实现
恶魔奶爸 指南帖——简易版
程序猿赚的那点钱算个P啊!
Usage of MySQL subquery keywords (exists)
现在网上开户安全么?想知道我现在在南宁,到哪里开户比较好?
Introduction to referer and referer policy
Is embedded system really safe? [how does onespin comprehensively solve the IC integrity problem for the development team]
Jetty: configure connector [easy to understand]
Codesonar Webinar
Object-C programming tips timer "suggestions collection"
[200 opencv routines] 223 Polygon fitting for feature extraction (cv.approxpolydp)
Arlo's troubles
数值法求解最优控制问题(〇)——定义
Demon daddy A3 stage near normal speed speech flow initial contact
Alibaba cloud award winning experience: how to mount NAS file system through ECS
[C language] advanced pointer --- do you really understand pointer?
Numerical method for solving optimal control problem (0) -- Definition