给定两棵树T1和T2。如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是“同构”的。例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A、B、G的左右孩子互换后,就得到另外一棵树。而图2就不是同构的。
图1
图2
输入格式:
输入给出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
输入样例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
#include<iostream> #define maxtree 10 using namespace std; typedef struct TreeNode { char data; int left; int right; }BiTree; BiTree B1[maxtree], B2[maxtree]; int BuildTree(BiTree B[],int N1); int isomorphic(int a, int b); int main(void) { int root1, root2; int state; int N1, N2; cin >> N1; root1 = BuildTree(B1,N1); //cout << root1<<endl; cin >> N2; root2 = BuildTree(B2,N2); //cout << root2<<endl; if (N1 > 0 && N2 > 0 && N1 == N2) { state = isomorphic(root1, root2); } if (N1 ==0 && N2 == 0) { state = 1; } else if (N1 != N2) { state = 0; } if (state == 1) { cout << "Yes"; } else { cout << "No"; } return 0; } int BuildTree(BiTree B[],int N) { char left, right; int check[maxtree] = { 0 }; for (int i = 0; i < N; ++i) { cin >> B[i].data >> left >> right; if (left != '-') { B[i].left = left - '0'; check[B[i].left] = 1; } else { B[i].left = -1; } if (right != '-') { B[i].right = right - '0'; check[B[i].right] = 1; } else { B[i].right = -1; } } //for (int i = 0; i < N; ++i) //{ // cout << B[i].data; //} //cout << endl; for (int i = 0; i < N; ++i) { if (check[i]==0) { return i; } } } int isomorphic(int a, int b) { if (B1[a].data !=B2[b].data) { return 0;//根结点不一样,不同构; } else { if (B1[a].left == -1 && B1[a].right == -1) //B1左右孩子都没有 { //if (B2[b].left == -1 && B2[b].right == -1) //{ // return 1; //} if (B2[b].left != -1 || B2[b].right != -1) { return 0; } } if (B1[a].left != -1 && B1[a].right == -1) //B1有左孩子 没有右孩子 { if ((B2[b].left == -1 && B2[b].right == -1) || (B2[b].left != -1 && B2[b].right != -1)) { return 0; } if (B2[b].left != -1 && B2[b].right == -1) { if (B1[B1[a].left].data != B2[B2[a].left].data) { return 0; } else { isomorphic(B1[a].left, B2[b].left); } } if (B2[b].left == -1 && B2[b].right != -1) { if (B1[B1[a].left].data != B2[B2[b].right].data) { return 0; } else { B2[b].left = B2[b].right; B2[b].right = -1; isomorphic(B1[a].left, B2[b].left); } } } if (B1[a].left == -1 && B1[a].right != -1)//B1没有左孩子 有右孩子 { if ((B2[b].left == -1 && B2[b].right == -1) || (B2[b].left != -1 && B2[b].right != -1)) { return 0; } if (B2[b].left != -1 && B2[b].right == -1) { if (B1[B1[a].right].data != B2[B2[b].left].data) { return 0; } else { B2[b].right = B2[b].left; B2[b].left = -1; isomorphic(B1[a].right, B2[b].right); } } if (B2[b].left == -1 && B2[b].right != -1) { if (B1[B1[a].right].data != B2[B2[b].right].data) { return 0; } else { isomorphic(B1[a].right, B2[b].right); } } } if (B1[a].left != -1 && B1[a].right != -1)//B1有左孩子 有右孩子 { if (B2[b].left == -1 || B2[b].right == -1) { return 0; } else { if ((B1[B1[a].left].data == B2[B2[b].left].data) && (B1[B1[a].right].data == B2[B2[b].right].data)) { isomorphic(B1[a].left, B2[b].left); isomorphic(B1[a].right, B2[b].right); } if ((B1[B1[a].left].data == B2[B2[b].right].data) && (B1[B1[a].right].data == B2[B2[b].left].data)) { int temp; temp = B2[b].right; B2[b].right = B2[b].left; B2[b].left = temp; isomorphic(B1[a].left, B2[b].left); isomorphic(B1[a].right, B2[b].right); } else { return 0; } } } } return 1; }