Then the meaning of the questions, the figure is not symmetrical binary tree, only a node in the subtree is 7;
Popular, a symmetrical binary tree that has a node for the subtree rooted x symmetry axis passing through the point x and the point of symmetry axis of symmetry of size must be equal on both sides, then this is a symmetrical binary tree.
Idea is very simple: recursively processing each child node of the tree nodes size, and then enumerate sub-tree for each node to determine whether a symmetrical binary tree. If one side there is a node on the other side did not return pruning once met on a different node pruning.
In particular, a single node is a symmetrical binary tree.
Code:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define LL long long using namespace std; int read(){//快读 int k = 0, f = 1; char c = getchar(); while(c < '0' || c > '9'){ if(c == '-') f = -1; c = getchar(); } while(c >= '0' && c <= '9'){ k = k * 10 + c - 48; c = getchar(); } return k * f; } int val[1000010], size[1000010], ans; struct zzz { int l, r; }node[1000010]; int dfs(int pos) {// if(pos == -1) return 0; size[pos] = dfs(node[pos].l) + dfs(node[pos].r) + 1; return size[pos]; } bool duichen(int ls, int rs) {//判断对称 if(ls == rs && ls == -1) return 1; if(val[ls] == val[rs] && size[ls] == size[rs] && duichen(node[ls].l, node[rs].r) && duichen(node[ls].r, node[rs].l)) return 1; return 0; } int main(){ int n = read(); for(int i = 1; i <= n; ++i) val[i]=read(); for(int i = 1; i <= n; ++i) node[i].l = read(), node[i].r = read(); dfs(1); for(int i = 1; i <= n; ++i) if(duichen(node[i].l, node[i].r)) ans = max(ans, size[i]); printf("%d\n", ans); return 0; }
end