题目:
![]()
输入样例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>
#include<cstdlib>
#include<cstdio>
using namespace std;
#define Null -1
#define Tree int
#define MaxTree 10
struct TreeNode
{
char Element;
Tree Left;
Tree Right;
}tree1[MaxTree], tree2[MaxTree];
Tree BuildTree(struct TreeNode T[])
{
int n, i, root = Null;
char left, right;
cin >> n;
if(n)
{
int check[MaxTree+1] = {0};
for(i=0; i<n; i++){
cin >> T[i].Element >> left >> right;
//判断左节点
if(left == '-')
T[i].Left = Null; //该节点的左节点为空
else{
T[i].Left = left - '0';
check[T[i].Left] = 1; //证明下标为T[i].Left的节点不是根节点
}
//判断右节点
if(right == '-')
T[i].Right = Null;
else{
T[i].Right = right - '0';
check[T[i].Right] = 1;
}
}
for(i=0; i<n; i++)
if(!check[i]) //寻找根节点
break;
root = i;
}
return root;
}
int Isomorphic(Tree R1, Tree R2)
{
//判断是否同构,结构是否相等(左右子树是否都可以遍历 + 元素相等)
//1-3:递归结束的条件
//1.两个树均为空,同构,返回1。
if((R1==Null) && (R2==Null))
return 1;
//2.一棵树为空,另外一棵树不为空,不同构,返回0。
if( ((R1==Null) && (R2!=Null)) || ((R1!=Null)&&(R2==Null)) )
return 0;
//3.两棵树的根节点不相等,不同构。
if(tree1[R1].Element != tree2[R2].Element)
return 0;
//4.两棵树的左子树均为空,则判断右子树是否同构。
if(tree1[R1].Left == Null && tree2[R2].Left == Null)
return Isomorphic(tree1[R1].Right, tree2[R2].Right);
//5.左子树都不空,且双方左子树树根的元素值相等,判断左左同构,右右同构
if((tree1[R1].Left!=Null && tree2[R2].Left!=Null) && tree1[tree1[R1].Left].Element==tree2[tree2[R2].Left].Element)
return (Isomorphic(tree1[R1].Left, tree2[R2].Left) && Isomorphic(tree1[R1].Right, tree2[R2].Right));
//6.左子树都不空,且双方左子树树根的元素值不相等,判断左右同构,右左同构。
else
return (Isomorphic(tree1[R1].Left, tree2[R2].Right) && Isomorphic(tree1[R1].Right, tree2[R2].Left));
}
int main()
{
Tree tree11, tree22;
tree11 = BuildTree(tree1);
tree22 = BuildTree(tree2);
if(Isomorphic(tree11, tree22))
cout << "Yes" << '\n' ;
else
cout << "No" << '\n' ;
return 0;
}