给定两棵树T1和T2。如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是“同构”的。例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A、B、G的左右孩子互换后,就得到另外一棵树。而图2就不是同构的。
图1
图2
现给定两棵树,请你判断它们是否是同构的。
输入格式:
输入给出2棵二叉树树的信息。对于每棵树,首先在一行中给出一个非负整数NN (\le 10≤10),即该树的结点数(此时假设结点从0到N-1N−1编号);随后NN行,第ii行对应编号第ii个结点,给出该结点中存储的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
输入样例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
/*
1,为什么#2处输入a 1 2,而cl存的是a,不是1,;cr存的是1,不是2?
答:在#1处把scanf("%d",&n); 改成scanf("%d\n",&n);即加一个\n,因为输入的数据格式有回车换行,而scanf要加入换行才能识别回车,
如果不加,那么#2处element读的是回车,cl 读的是a,cr读的是1;
这么做,#2处也得由scanf("%c %c %c",&t[i].element,&cl,&cr);加一个\n;这样做,可以实现功能,但是又有一个新问题,就是最后得加多
一个字母才能是输入结束;
最终解决方法:在scanf后面加个fflush(sdtin);用于清空缓冲区;具体原因得参见关于scanf的博客;
2,elment的值在哪里可以看到?
*/
#include <stdio.h>
#include <stdlib.h>
#define maxTree 10
#define elementType char
#define tree int
#define Null -1
struct treeNode{
elementType element;
tree left;
tree right;
}t1[maxTree],t2[maxTree];
tree buildTree(struct treeNode t[]);
tree lsomorphism(tree r1,tree r2);
int main()
{
tree r1;
tree r2; //注意这里不要用 tree r1,r2;因为前面是define,这是r2就不是int类型,也不知道是什么类型
// printf("%d",Null);
r1 = buildTree(t1);
r2 = buildTree(t2);
// printf("%d %d",r1,r2);
if(lsomorphism(r1,r2)){
printf("Yes\n");
}
else{
printf("No\n");
}
return 0;
}
tree buildTree(struct treeNode t[]){
int n;
scanf("%d\n",&n); //#1
int root;
if (n){
int i;
char cl,cr;
int check[maxTree];
for(i=0;i<n;i++){
check[i] = 0;
}
for (i=0;i<n;i++){
scanf("%c %c %c",&t[i].element,&cl,&cr); // #2 注意这里是t[i],不是t1[i],或者t2[i],因为是函数的代码,t1,t2是传进来的
fflush(stdin);//刷新缓冲区
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++){
if(!check[i]){
break;
}
}
root = i;
}
return root;
}
tree lsomorphism(tree r1,tree r2){
if (r1 == Null && r2 == Null){
return 1;
}
else if (((r1 == Null )&& (r2 != Null ))|| ((r1 != Null) && (r2 == Null))){
return 0;
}
else if (t1[r1].element != t2[r2].element){
return 0;
}
else if((t1[r1].left) == Null && (t2[r2].left == Null)){
return lsomorphism(t1[r1].right,t2[r2].right);
}
else if((t1[r1].left != Null)&&(t2[r2].left != Null)&&((t1[t1[r1].left].element)== (t2[t2[r2].left].element))){
return (lsomorphism(t1[r1].left,t2[r2].left)&&lsomorphism(t1[r1].right,t2[r2].right));
}
else{
return (lsomorphism(t1[r1].left,t2[r2].right)&&lsomorphism(t1[r1].right,t2[r2].left));
}
}