题目
现给定两棵树,请你判断它们是否是同构的。
输入格式:
输入给出2棵二叉树树的信息。对于每棵树,首先在一行中给出一个非负整数N (≤10),即该树的结点数(此时假设结点从0到N−1编号);随后N行,第i行对应编号第i个结点,给出该结点中存储的1个英文大写字母、其左孩子结点的编号、右孩子结点的编号。如果孩子结点为空,则在相应位置上给出“-”。给出的数据间用一个空格分隔。注意:题目保证每个结点中存储的字母是不同的。
输出格式:
如果两棵树是同构的,输出“Yes”,否则输出“No”。
输入样例1(对应图1):
8
A 1 2
B 3 4
C 5 -
D - -
E 6 -
G 7 -
F - -
H - -
8
G - 4
B 7 6
F - -
A 5 1
H - -
C 0 -
D - -
E 2 -
输出样例1:
Yes
分析:
判断树的同构就是要判断,树的左右子树交换位置后,是否相等。
对于本题的输入形式,可以采取三维数组的形式去存储一棵树。
struct TreeNode
{
ElementType Element;
Tree Left;
Tree Right;
}T1[MaxTree],T2[MaxTree];//定义两个全局结构数组
对于程序的框架比较简单:
建立两棵树,然后判断是否同构即可。
int main(void)
{
Tree R1, R2;
R1 = BuildTree(T1);//To construct two Trees and the return of the Root;
R2 = BuildTree(T2);
if (Isomorphic(R1, R2))
{
printf("Yes\n");
}
return 0;
}
接下来就是树的输入存储问题了。
我们在前面已经说明了,用一个二维数组来存储树。
所以对于树的输入,就是直接用循环进行输入就行了。
Tree BuildTree(TreeNode *T)
{
int N,index;
int Root = Null;
int check[MaxTree];
for (int i = 0; i < MaxTree; i++)
{
check[i] == 0;
}
char cl, cr;
scanf("%d", &N);
if (N)
{
for(int i=0;i<N;i++)
{
scanf("%c %c %c", &T[i].Element, &cl, &cr);
if (cl == '-')
{
T[i].Left = Null;
}
else
{
T[i].Left = '-' - '0';
check[T[i].Left] = 1;
}
if (cr == '-')
{
T[i].Right = Null;
}
else
{
T[i].Right = '-' - '0';
check[T[i].Right] = 1;
}
}
for (index = 0; index < N; index++)
{
if (check[index] == 0)
break;
}
Root = index;
}
return Root;
}
在输入中,我们输入的全是字符表示,所以为了能更好的找到一个结点的左右孩子,我们需要把左右孩子的字符数字转换为真正的数字。而对于空孩子,只需把-转化为-1,就行了。
在这个函数中,我们返回的是树根的位置,因为,在判断树是否同构是要从根结点开始的 ,所以我们需要找到这个根结点的位置并返回。
我们会发现,因为输入的结点并非是按树的结点顺序从上至下的输入,这对我们找树根结点产生了困难,但是可以观察,根结点所在数组的索引并不会出现在各孩子的数组中。所以我们可以设置一个Check[MAX]数组来标记已被使用的数组的索引,设置它初始值为0,如果被标记了,就把元素设置为1.
树同构判断函数。
int Isomorphic(Tree R1, Tree R2)
{
if (R1 == Null && R2 == Null)
return 1;
if ((R1 == Null && R2 != Null) || (R1 != Null && R2 == Null))
return 0;
if (T1[R1].Element != T2[R2].Element)
return 0;
if ((T1[R1].Left == Null) && T2[R2].Left == Null)
return Isomorphic(T1[R1].Right, T2[R2].Right);
if (((T1[R1].Left != Null) && (T2[R2].Left != Null)) && ((T1[T1[R1].Left].Element) == (T2[T1[R2].Left].Element)))
/*No need to swap the left and the rigth*/
{
return((Isomorphic(T1[R1].Left, T2[R2].Left)) && (Isomorphic(T1[R1].Right, T2[R2].Right)));
}
else
return((Isomorphic(T1[R1].Left, T2[R2].Right)) && (Isomorphic(T1[R1].Left, T2[R2].Right)));
/*Need to swap the left and the right*/
}
说明
在这里可分为以下几种情况:
- 根结点为空,直接返回1
- 根结点有一个为空,返回0
- 根结点不相等,返回0
- 都没有左孩子,递归调用判断右子树
- 根结点不为空且相等,左孩子相等,递归调用,判断左子树和右子树
- 根结点不为空且相等,左孩子不相等,交换位置,递归判断,左子树和右子树
全部代码:
#include<stdio.h>
#include<stdlib.h>
#define MaxTree 10
typedef char ElementType;
typedef int Tree;
#define Null -1
struct TreeNode
{
ElementType Element;
Tree Left;
Tree Right;
}T1[MaxTree],T2[MaxTree];//定义两个全局结构数组
Tree BuildTree(TreeNode *T)
{
int N,index;
int Root = Null;
int check[MaxTree];
for (int i = 0; i < MaxTree; i++)
{
check[i] == 0;
}
char cl, cr;
scanf("%d", &N);
if (N)
{
for(int i=0;i<N;i++)
{
scanf("%c %c %c", &T[i].Element, &cl, &cr);
if (cl == '-')
{
T[i].Left = Null;
}
else
{
T[i].Left = '-' - '0';
check[T[i].Left] = 1;
}
if (cr == '-')
{
T[i].Right = Null;
}
else
{
T[i].Right = '-' - '0';
check[T[i].Right] = 1;
}
}
for (index = 0; index < N; index++)
{
if (check[index] == 0)
break;
}
Root = index;
}
return Root;
}
int Isomorphic(Tree R1, Tree R2)
{
if (R1 == Null && R2 == Null)
return 1;
if ((R1 == Null && R2 != Null) || (R1 != Null && R2 == Null))
return 0;
if (T1[R1].Element != T2[R2].Element)
return 0;
if ((T1[R1].Left == Null) && T2[R2].Left == Null)
return Isomorphic(T1[R1].Right, T2[R2].Right);
if (((T1[R1].Left != Null) && (T2[R2].Left != Null)) && ((T1[T1[R1].Left].Element) == (T2[T1[R2].Left].Element)))
/*No need to swap the left and the rigth*/
{
return((Isomorphic(T1[R1].Left, T2[R2].Left)) && (Isomorphic(T1[R1].Right, T2[R2].Right)));
}
else
return((Isomorphic(T1[R1].Left, T2[R2].Right)) && (Isomorphic(T1[R1].Left, T2[R2].Right)));
/*Need to swap the left and the right*/
}
int main(void)
{
Tree R1, R2;
R1 = BuildTree(T1);//To construct two Trees and the return of the Root;
R2 = BuildTree(T2);
if (Isomorphic(R1, R2))
{
printf("Yes\n");
}
return 0;
}