#include<stdio.h>
typedef struct node{
int boo;
struct node *chil;
int l;
struct node *chir;
int r;
};
int bl(node *p);
int bj(node *q);
int bk(node *r);
int main(){
struct node k[16];
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
k[i].boo=i;
}
for(int i=1;i<=n;i++){
k[i].boo=i;
int a,b;
scanf("%d %d",&a,&b);
if(a != 0){
k[i].chil=&k[a];
k[i].l=1;
}else{
k[i].l=0;
}
if(b != 0){
k[i].chir=&k[b];
k[i].r=1;
}else{
k[i].r=0;
}
}
//输入数据
bl(&k[1]);
printf("\n");
bj(&k[1]);
printf("\n");
bk(&k[1]);
return 0;
}
int bl(node *p){
printf("%d ",p->boo);
if((p->l) == 1){
bl(&(*p->chil));
}
if((p->r) == 1){
bl(&(*p->chir));
}
}
int bj(node *q){
if((q->l)==1){
bj(&(*q->chil));
}
printf("%d ",q->boo);
if((q->r)==1){
bj(&(*q->chir));
}
}
int bk(node *r){
if((r->l)==1){
bk(&(*r->chil));
}
if((r->r)==1){
bk(&(*r->chir));
}
printf("%d ",r->boo);
}
原文地址:http://blog.51cto.com/13986036/2280971