#include<stdio.h>
#define N 20
#define Null -1
int First;
struct TreeNode
{
char Element;
int Left,Right;
}Tree1[N],Tree2[N];
int main()
{
int BuildTree(struct TreeNode T[]);
int isomorphic(int F1, int F2);
int F1,F2;
F1=BuildTree(Tree1);
F2=BuildTree(Tree2);
if(isomorphic(F1,F2)==1)
printf("Yes\n");
else
printf("No\n");
return 0;
}
int BuildTree(struct TreeNode T[])
{
int n;
int check[N]={0};
int Root=Null;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
getchar();
char cl,cr;
scanf("%c %c %c",&T[i].Element,&cl,&cr);
if(cl!='-')
{
T[i].Left=cl-'0';
check[T[i].Left]=1;
}
else
T[i].Left=Null;
if(cr!='-')
{
T[i].Right=cr-'0';
check[T[i].Right]=1;
}
else
T[i].Right=Null;
}
if(n)
{
for(int i=0;i<n;i++)
{
if(check[i]==0)
{
Root=i;
break;
}
}
}
return Root;
}
int isomorphic(int F1, int F2)
{
if(F1==Null&&F2==Null)
return 1;
if(F1==Null&&F2!=Null || F1!=Null&&F2==Null)
return 0;
if(Tree1[F1].Element!=Tree2[F2].Element)
return 0;
if(Tree1[F1].Left==Null&&Tree2[F2].Left==Null)
return isomorphic(Tree1[F1].Right,Tree2[F2].Right);
if(Tree1[F1].Left!=Null&&Tree2[F2].Left!=Null &&
Tree1[Tree1[F1].Left].Element==Tree2[Tree2[F2].Left].Element)
return (isomorphic(Tree1[F1].Left, Tree2[F2].Left)&&
isomorphic(Tree1[F1].Right, Tree2[F2].Right));
else
return (isomorphic(Tree1[F1].Left, Tree2[F2].Right)&&
isomorphic(Tree1[F1].Right, Tree2[F2].Left));
}
树的同构
最新推荐文章于 2021-06-14 19:07:53 发布