通过createtree函数,tree数组中每个元素的值都是以这个元素为根节点,左右子树的节点数量(包含根节点)。
判断一棵子树是否符合对称二叉树需要满足以下几点:1.根节点有左右孩子且权值相等 2.根节点没有左右孩子

#include <iostream>
using namespace std;
const int maxn = 1000010;
int val[maxn];//每个节点的权值
int lc[maxn];//左孩子
int rc[maxn];//右孩子
int tree[maxn];//记录每个根节点有几个孩子
int res;
int createtree(int root) {
    tree[root] = 1;
    if (lc[root] != -1) {
        tree[root] += createtree(lc[root]);
    }
    if (rc[root] != -1) {
        tree[root] += createtree(rc[root]);
    }
    return tree[root];
}
bool check(int root1, int root2) {
    if (root1 != -1 && root2 != -1) {
        if (val[root1] != val[root2])return false;
        return check(lc[root1], rc[root2]) && check(rc[root1], lc[root2]);
    }
    else if (root1 == -1 && root2 == -1)return true;
    else return false;
}
void calc(int root) {

    if (root == -1)return; // if (root == -1||tree[root]<res)return;
    if (check(root, root))res = max(res, tree[root]);
    calc(lc[root]);
    calc(rc[root]);
}
int main()
{
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++)cin >> val[i];
    for (int i = 1; i <= n; i++) {
        cin >> lc[i] >> rc[i];
    }
    //createtree(1);
    calc(1);
    cout << res;
}