3340 数据结构实验之二叉树一:树的同构

来源:互联网 发布:软件企业认定有效期 编辑:程序博客网 时间:2024/05/29 14:22

数据结构实验之二叉树一:树的同构

#include<bits/stdc++.h>  using namespace std;  struct tree  {      char c;      struct tree *lc,*rc;  };  struct node  {      char s;      int l,r;  } a[15];  struct tree * creat(int k)  {      struct tree *t;      t=(struct tree *)malloc(sizeof(struct tree));      t->lc=t->rc=NULL;      t->c=a[k].s;      if(a[k].l!=-1)          t->lc=creat(a[k].l);      if(a[k].r!=-1)          t->rc=creat(a[k].r);      return t;  };  struct tree *build(int n)  {      int j;      bool v[15];      memset(v,false,sizeof(v));      for(int i=0; i<n; i++)      {          char s1[10],s2[10],s3[10];          scanf("%s%s%s",s1,s2,s3);          a[i].s=s1[0];          if(s2[0]=='-')              a[i].l=-1;          else          {              a[i].l=s2[0]-'0';              v[a[i].l]=true;          }          if(s3[0]=='-')              a[i].r=-1;          else          {              a[i].r=s3[0]-'0';              v[a[i].r]=true;          }      }      if(n!=0)      {          for(j=0; j<n; j++)              if(!v[j])                  break;      }      struct tree *root1=creat(j);      return root1;  };  int ju(struct tree *t1,struct tree *t2)  {      if(t1==NULL&&t2==NULL)          return 1;      if(t1!=NULL&&t2!=NULL)          if(t1->c==t2->c)              if((ju(t1->lc,t2->lc)&&ju(t1->rc,t2->rc))||(ju(t1->rc,t2->lc)&&ju(t1->lc,t2->rc)))                  return 1;      return 0;  }  int main()  {      int n;      while(~scanf("%d",&n))      {          struct tree *root1=build(n);          int m;          scanf("%d",&m);          struct tree *root2=build(m);          if(ju(root1,root2))              printf("Yes\n");          else              printf("No\n");      }  }
0 0