PAT 树的同构问题(新人求解)
为什么我的程序编译执行没问题,但是建了一棵树之后程序就弹出了呢?
源程序如下
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
#define MaxTree 10
#define Null -1
typedef int Tree;
typedef char ElementType;
struct TreeNode
{ ElementType element;
Tree left;
Tree right;
}T1[MaxTree],T2[MaxTree];
Tree BuildTree( struct TreeNode T[] );
Tree Isomorphic(Tree R1,Tree R2);
int main()
{ Tree R1,R2;
R1=BuildTree(T1);
R2=BuildTree(T2);
if(Isomorphic(R1,R2))
printf("yes\n");
else
printf("no\n");
return 0;
}
BuildTree( struct TreeNode T[] )
{ int check[10];
int N,Root;
char cl,cr;
scanf("%d\n",&N);
if(N){
for(int i=0;i<N;i++)
check[i]=0;
for(int m=0;m<N;m++)
{ scanf("%c%c%c\n",&T[m].element,&cl,&cr);
if(cl!='-')
{ T[m].left=cl-'0';
check[T[m].left]=1;
}
else T[m].left=Null;
if(cr!='-')
{ T[m].right=cr-'0';
check[T[m].right]=1;
}
else T[m].right=Null;
}
for(int n=0;n<N;n++)
{ if(check==0)
break;
}
Root=n;}
return Root;
}
Tree Isomorphic(Tree R1,Tree R2)
{ struct TreeNode T[MaxTree];
if((R1==Null)&&(R2==Null))
return 1;
if(((R1!=Null)&&(R2==Null))||((R1==Null)&&(R2!=Null)))
return 0;
if(T[R1].element!=T[R2].element)
return 0;
if((T[R1].left==Null)&&(T[R2].left==Null))
return Isomorphic(T[R1].right,T[R2].right);
if(((T[R1].left!=Null)&&(T[R2].left!=Null))&&((T[T[R1].left].element)==(T[T[R2].left].element)))
return (Isomorphic(T[R1].left,T[R2].left)&&Isomorphic(T[R1].right,T[R2].right));
else
return (Isomorphic(T[R1].left,T[R2].right)&&Isomorphic(T[R1].right,T[R2].left));
}