当前位置:网站首页>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
边栏推荐
- Codeforces Round #296 (Div. 2) A. Playing with Paper[通俗易懂]
- 恶魔奶爸 B3 少量泛读,完成两万词汇量+
- The latest version of codesonar has improved functional security and supports Misra, c++ parsing and visualization
- HDU4876ZCC loves cards(多校题)
- What are the official stock trading apps in the country? Is it safe to use
- MinGW MinGW-w64 TDM-GCC等工具链之间的差别与联系「建议收藏」
- DataTable数据转换为实体
- AADL inspector fault tree safety analysis module
- Klocwork code static analysis tool
- 浅解ARC中的 __bridge、__bridge_retained和__bridge_transfer
猜你喜欢
[200 opencv routines] 223 Polygon fitting for feature extraction (cv.approxpolydp)
Small guide for rapid formation of manipulator (11): standard nomenclature of coordinate system
使用高斯Redis实现二级索引
Ubuntu安装mysql8遇到的问题以及详细安装过程
Onespin | solve the problems of hardware Trojan horse and security trust in IC Design
Cantata9.0 | new features
Klocwork code static analysis tool
Small guide for rapid formation of manipulator (12): inverse kinematics analysis
MySQL约束之默认约束default与零填充约束zerofill
The new version of onespin 360 DV has been released, refreshing the experience of FPGA formal verification function
随机推荐
刚开户的能买什么股票呢?炒股账户安全吗
Is private equity legal in China? Is it safe?
Le capital - investissement est - il légal en Chine? C'est sûr?
A brief understanding of the in arc__ bridge、__ bridge_ Retained and__ bridge_ transfer
Codesonar enhances software reliability through innovative static analysis
[function recursion] do you know all five classic examples of simple recursion?
I have to use my ID card to open an account. Is the bank card safe? I don't understand it
恶魔奶爸 A1 语音听力初挑战
Spark judges that DF is empty
Tensorflow2. How to run under x 1 Code of X
How can big state-owned banks break the anti fraud dilemma?
201215-03-19—cocos2dx内存管理–具体解释「建议收藏」
ISO 26262 - considerations other than requirements based testing
FTP steps for downloading files from Huawei CE switches
【OpenCV 例程200篇】223. 特征提取之多边形拟合(cv.approxPolyDP)
Is it safe to open an account online now? I want to know where I can open an account in Nanning now?
[paper reading] maps: Multi-Agent Reinforcement Learning Based Portfolio Management System
Description of the difference between character varying and character in PostgreSQL database
Magic weapon - sensitive file discovery tool
The latest version of codesonar has improved functional security and supports Misra, c++ parsing and visualization