#include <stdio.h>
#define Null -1
typedef int Tree;
typedef char ElementType;
typedef struct TreeNode TNode;
struct TreeNode {
ElementType Data;
Tree Left;
Tree Right;
};
Tree getTree(TNode nodes[], int length) {
int flagArr[length];
for (int i = 0; i < length; i++)
flagArr[i] = 0;
char data, left, right;
for (int i = 0; i < length; i++) {
scanf(" %c %c %c", &data, &left, &right);//这里%c前要加空格,否则第一个接受的就是\n,真坑
TNode node;
node.Data = data;
if (left == '-') {
node.Left = Null;
} else {
node.Left = left - '0';
flagArr[node.Left] =1;
}
if (right == '-') {
node.Right = Null;
} else {
node.Right = right - '0';
flagArr[node.Right] =1;
}
nodes[i] = node;
}
Tree root = Null;
for (int i = 0; i < length; i++){
if(flagArr[i] == 0){
root = i;
break;
}
}
return root;
}
int Isomorphic(Tree R1,Tree R2,TNode nodes1[],TNode nodes2[]){
if(R1 == Null && R2 == Null){
return 1;
}else if((R1 == Null && R2 != Null) || (R1 != Null && R2 == Null)){
return 0;
}else if(nodes1[R1].Data != nodes2[R2].Data){
return 0;
}else if(nodes1[R1].Left == Null && nodes2[R1].Left == Null){
return Isomorphic(nodes1[R1].Right,nodes2[R2].Right,nodes1,nodes2);
}else if((nodes1[R1].Left != Null && nodes2[R2].Left != Null) && (nodes1[nodes1[R1].Left].Data == nodes2[nodes2[R2].Left].Data)){
int isLeft = Isomorphic(nodes1[R1].Left,nodes2[R2].Left,nodes1,nodes2);
int isRight = Isomorphic(nodes1[R1].Right,nodes2[R2].Right,nodes1,nodes2);
return isLeft && isRight;
}else{
int isLeft = Isomorphic(nodes1[R1].Left,nodes2[R2].Right,nodes1,nodes2);
int isRight = Isomorphic(nodes1[R1].Right,nodes2[R2].Left,nodes1,nodes2);
return isLeft && isRight;
}
}
//遍历数组元素,因为之前一直出错,所以就像看看输入的数据是否和预期一样,真实答案不需要这个方法
void printTree(Tree root,TNode nodes[]){
if(root != Null){
TNode node = nodes[root];
printf("%c %d %d\n",node.Data,node.Left,node.Right);
printTree(node.Left,nodes);
printTree(node.Right,nodes);
}
}
int main() {
int len1;
scanf("%d\n", &len1);
if(len1 < 0){
printf("No");
return 0;
}
TNode nodes1[len1];
Tree tree1 = getTree(nodes1,len1);//获取树
int len2;
scanf("%d\n", &len2);
if(len1 != len2){
printf("No");
return 0;
}
TNode nodes2[len2];
Tree tree2 = getTree(nodes2,len2);//获取树
int result = Isomorphic(tree1,tree2,nodes1,nodes2);//判断是否同构
if(result){
printf("Yes");
}else{
printf("No");
}
// printTree(tree1,nodes1);
return 0;
}