#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int y=0;
struct node
{
char data;
struct node *l,*r;
};
struct node *creat(struct node *root,char *a,char *b,int n)
{
int p;
if(n<=0)return NULL;
root=(struct node*)malloc(sizeof(struct node));
root->data=a[0];
p=strchr(b,a[0])-b;
root->l=creat(root->l,a+1,b,p);
root->r=creat(root->r,a+p+1,b+p+1,n-p-1);
return root;
}
void houxu(struct node *t)
{
if(t!=NULL)
{
houxu(t->l);
houxu(t->r);
printf("%c",t->data);
}
}
int shendu(struct node *t)
{
int dl,dr;
if(!t)
return 0;
else
{
dl=shendu(t->l);
dr=shendu(t->r);
return dl>dr?dl+1:dr+1;
}
}
void yezishu(struct node *t)
{
if(t)
{
if(t->l==NULL&&t->r==NULL)
y++;
yezishu(t->l);
yezishu(t->r);
}
}
int main()
{
char a[1000],b[1000];
int n;
struct node *root;
scanf("%s%s",a,b);
n=strlen(a);
root=creat(root,a,b,n);
houxu(root);
printf("\n%d\n",shendu(root));
yezishu(root);
printf("%d",y);
return 0;
}
已知先序中序求后序模版
最新推荐文章于 2022-04-14 00:24:57 发布