已知先序中序求后序模版

来源:互联网 发布:ubuntu 创建新文件夹 编辑:程序博客网 时间:2024/06/06 13:12
#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;}
0 0
原创粉丝点击