当前位置:网站首页>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
边栏推荐
- Jetty: configure connector [easy to understand]
- Is private equity legal in China? Is it safe?
- easyui 日期控件清空值
- SQL注入报错注入函数图文详解
- 【函数递归】简单递归的5个经典例子,你都会吗?
- uva 12230 – Crossing Rivers(概率)「建议收藏」
- awk处理JSON处理
- Unity3d 4.3.4f1执行项目
- Demon daddy B2 breaks through grammar and completes orthodox oral practice
- Hdu4876zcc love cards (multi check questions)
猜你喜欢
Helix QAC 2020.2 new static test tool maximizes the coverage of standard compliance
【OpenCV 例程200篇】223. 特征提取之多边形拟合(cv.approxPolyDP)
C语言多角度帮助你深入理解指针(1. 字符指针2. 数组指针和 指针数组 、数组传参和指针传参3. 函数指针4. 函数指针数组5. 指向函数指针数组的指针6. 回调函数)
H3C s7000/s7500e/10500 series post stack BFD detection configuration method
Small guide for rapid formation of manipulator (12): inverse kinematics analysis
Ubuntu安装mysql8遇到的问题以及详细安装过程
目标:不排斥 yaml 语法。争取快速上手
Default constraint and zero fill constraint of MySQL constraint
How to meet the dual needs of security and confidentiality of medical devices?
Demon daddy B3 read extensively in a small amount, and completed 20000 vocabulary+
随机推荐
Codeforces round 275 (Div. 2) C – diverse permutation (construction) [easy to understand]
95年专注安全这一件事 沃尔沃未来聚焦智能驾驶与电气化领域安全
Demon daddy C
Static test tool
Arlo's troubles
恶魔奶爸 A3阶段 近常速语流初接触
权限不足
Tensorflow2. How to run under x 1 Code of X
npm uninstall和rm直接删除的区别
Codesonar enhances software reliability through innovative static analysis
私募基金在中国合法吗?安全吗?
What are the official stock trading apps in the country? Is it safe to use
201215-03-19—cocos2dx内存管理–具体解释「建议收藏」
UVA 11080 – Place the Guards(二分图判定)
2022年在启牛开中银股票的账户安全吗?
Write a jump table
Make this crmeb single merchant wechat mall system popular, so easy to use!
guava多线程,futurecallback线程调用不平均
Hoj 2245 planktonic triangle cell (Mathematics)
国家正规的股票交易app有哪些?使用安不安全