不熟悉静态链表利用左右子节点下标建树,以及如何寻找根节点,使用静态链表建树传入根节点只要传入根节点下标。
#include<iostream>
#include<algorithm>
#include<stack>
#include<cstring>
using namespace std;
struct node
{
char data;
int left;
int right;
}T1[10],T2[10];
bool check[10];
int buildTree(node T[])
{
int root=-1, i;
int n; cin >> n;
for(int i=0; i<n; i++)check[i]=0;
char a, b;
for (i = 0; i < n; i++)
{
cin >> T[i].data >> a>> b;
if (a != '-')
{
T[i].left = a - '0';
check[T[i].left] = 1;
}
else
T[i].left = -1;
if (b != '-')
{
T[i].right = b - '0';
check[T[i].right] = 1;
}
else
T[i].right = -1;
}
for (i = 0; i < n; i++)
{
if (check[i] != 1)
{
root = i;
break;
}
}
return root;
}
bool judge(int R1, int R2) {
if (R1 == -1 && R2 == -1)
return true;
if (R1 == -1 && R2 != -1 || R1 != -1 && R2 == -1)
return false;
if (T1[R1].data != T2[R2].data)
return false;
return (judge(T1[R1].left, T2[R2].left) &&
judge(T1[R1].right, T2[R2].right))||
(judge(T1[R1].right, T2[R2].left) &&
judge(T1[R1].left, T2[R2].right));
}
int main()
{
int r1, r2;
r1 = buildTree(T1);
r2 = buildTree(T2);
if (judge(r1, r2))
cout << "Yes";
else
cout << "No";
return 0;
}