#include<stdio.h>
#include<stdlib.h>
struct node{
int x;
struct node *l,*r;
};
int qx[100],zx[100];
struct node *js(int qs,int qt,int zs,int zt){
if(qs>qt) return NULL;
struct node *h=new node;
h->x=qx[qs];
h->l=NULL;h->r=NULL;
if(qs==qt)
return h;
int pos;
for(int i=zs;i<=zt;i++)
if(zx[i]==qx[qs]){
pos=i;
break;
}
h->l=js(qs+1,qs+pos-zs,zs,pos-1);
h->r=js(qs+pos-zs+1,qt,pos+1,zt);
return h;
}
void out(struct node *h){
if(h)out(h->l);
if(h)out(h->r);
if(h)printf("%d ",h->x);
}
int main(){
int i,j,k,m,n;
scanf("%d",&n);
for(i=1;i<=n;i++)scanf("%d",&qx[i]);
for(i=1;i<=n;i++)scanf("%d",&zx[i]);
struct node *h=js(1,n,1,n);
out(h);
system("pause");
return 0;
}
已知中序前序求后序
最新推荐文章于 2021-01-17 15:06:08 发布