03 - 树 1 树的同构 (25 分)
<pre><small>给定两棵树 T1 和 T2。
如果 T1 可以通过若干次左右孩子互换就变成 T2,
则我们称两棵树是 “同构” 的。
例如图 1 给出的两棵树就是同构的,
因为我们把其中一棵树的结点 A、B、G 的左右孩子互换后,
就得到另外一棵树。而图 2 就不是同构的。</small></pre>
Paste_Image.png
Paste_Image.png
<pre><small>
现给定两棵树,请你判断它们是否是同构的。
输入格式:
输入给出 2 棵二叉树树的信息。
对于每棵树,首先在一行中给出一个非负整数N (≤10),
即该树的结点数(此时假设结点从 0 到N−1 编号);
随后N行,第i 行对应编号第i 个结点,
给出该结点中存储的 1 个英文大写字母、
其左孩子结点的编号、
右孩子结点的编号。
如果孩子结点为空,则在相应位置上给出 “-”。
给出的数据间用一个空格分隔。
注意:题目保证每个结点中存储的字母是不同的。
输出格式:
如果两棵树是同构的,输出 “Yes”,否则输出 “No”。
输入样例 1(对应图 1):
</small></pre>
<pre><small>
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
</small></pre>
<pre><small>
输入样例 2(对应图 2):
8
B 5 7
F - -
A 0 3
C 6 -
H - -
D - -
G 4 -
E 1 -
8
D 6 -
B 5 -
E - -
H - -
C 0 2
G - 3
F - -
A 1 4
输出样例 2:
No</small></pre>
<pre><small>
include <stdio.h>
define OK 1
define ERROR 0
define MaxTree 10
define Null -1
//区别于系统的NULL 0
define ElementType char
typedef int Status;
typedef struct TreeNode
{
ElementType data;
int left;
int right;
} Tree;
Tree T1[MaxTree], T2[MaxTree];
</small></pre>
<pre><small>
int BulidTree(Tree T[])
{
int N, check[MaxTree], root = Null;
//root = Null 空树则返回Null
char cl, cr;
//左右孩子序号
int i;
scanf("%d\n",&N);
if(N) {
for(i = 0; i < N; i++)
check[i] = 0;
for(i = 0; i < N; i++) {
scanf("%c %c %c\n",&T[i].data,&cl,&cr);
//找root
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;
}
}
for( i = 0; i < N; i++)
//check[]=0的为根节点
if(!check[i]) {
root = i;
break;
}
}
return root;
}
</small></pre>
<pre><small>
Status Isomprphic(int root1, int root2)
{
if( (root1 == Null) && (root2 == Null))
//都是空 ,同构
return OK;
if( (root1 == Null)&&(root2 != Null) || (root1 != Null)&&(root2 == Null))//其中一个为空,不同构
return ERROR;
if(T1[root1].data != T2[root2].data)
//根数据不同,不同构
return ERROR;
if( (T1[root1].left == Null) && (T2[root2].left == Null) )
//左子树为空,则判断右子树
return Isomprphic(T1[root1].right, T2[root2].right);
if((T1[root1].left != Null) && (T2[root2].left != Null) &&
( T1[T1[root1].left].data == T2[T2[root2].left].data) )
//两树左子树皆不空,且值相等
return (Isomprphic(T1[root1].left, T2[root2].left) &&
//判断其子树
Isomprphic(T1[root1].right, T2[root2].right) );
else //两树左子树有一个空 或者 皆不空但值不等
return (Isomprphic(T1[root1].left, T2[root2].right) &&
//交换左右子树判断
Isomprphic(T1[root1].right, T2[root2].left) );
}
</small></pre>
<pre><small>
int main()
{
int root1, root2;
root1 = BulidTree(T1);
root2 = BulidTree(T2);
if(Isomprphic(root1, root2) )
printf("Yes\n");
else
printf("No\n");
return 0;
}
</small></pre>