判断两个树是否相同

题目为树的同构
递归思路,先处理基本情况,然后在处理左左相等就看右右相不相等,如果左左不相等就看左右互换的情况

#define MaxTree 10
#define ElementType char
#define Tree int
#define Null -1

struct TreeNode {
    ElementType Element;
    Tree left;
    Tree right;
}T1[MaxTree], T2[MaxTree];

int buildTree(struct TreeNode T1[]);
int comp(Tree r1, Tree r2);
int main(){
    Tree r1, r2;
    r1 = buildTree(T1);
    r2 = buildTree(T2);
    if(comp(r1, r2)) {
        printf("Yes");
    }else{
        printf("No");
    }
}

int buildTree(struct TreeNode T[]){
    char tl, tr;
    int root = Null, n;
    scanf("%dn", &n);
    int check[n];
    for(int i=0;i<n;i++)check[i]=0;
    for(int i=0;i<n;i++){
        scanf("%c %c %cn", &T[i].Element, &tl, &tr);
        if(tl=='-'){
            T[i].left = Null;
        }else {
            T[i].left = tl-'0';
            check[T[i].left]  = 1;
        }

        if(tr=='-'){
            T[i].right = Null;
        }else{
            T[i].right = tr - '0';
            check[T[i].right]  = 1;
        }
    }

    for(int i=0;i<n;i++){
        if(check[i]==0){
            root = i;
            break;
        }
    }
    return root;
}

int comp(Tree root1, Tree root2) {
    //节点都为空
    if((root1==Null) && (root2==Null)) {
        return 1;
    }
    //节点可以一个为空一个不为空
    if((root1==Null) && (root2!=Null)) {
        return 0;
    }

    if((root1!=Null) && (root2==Null)) {
        return 0;
    }
    //节点元素不相等
    if((T1[root1].Element) != (T2[root2].Element)) {
        return 0;
    }

    //节点左元素都为空
    if((T1[root1].left == Null) && (T2[root2].left == Null)) {
        return comp(T1[root1].right, T2[root2].right);
    }

    //左边元素都不为空
    if(((T1[root1].left != Null) && (T2[root2].left != Null)) && 
      (T1[T1[root1].left].Element == T2[T2[root2].left].Element)) {
        return comp(T1[root1].right, T2[root2].right);
    }else {
        return (comp(T1[root1].right, T2[root2].left) && comp(T1[root1].left, T2[root2].right));
    }
}