Luo Gu P5018 symmetrical binary tree (search)

Ok...

 

Topic link: https: //www.luogu.org/problem/P5018

 

In fact, this question can be found directly out of the search:

First, the number of recursion Son as the root node for each point out an initialization, and then look at the sub-tree root node as the tree is not symmetrical, then the maximum value recorded in the subtree of symmetry.

 

AC Code:

 1 #include<cstdio>
 2 #include<iostream>
 3 
 4 using namespace std;
 5 
 6 struct node{
 7     int l, r, v, len;
 8     node() {l = -1; r = -1;}
 9 } e[1000005];
10 
11 int n, ans;
12 
13 inline int dfs(int k){
14     e[k].len = 1;
15     if(e[k].l != -1) e[k].len +=DFS (E [K] .L);
 16      IF (! E [K] .r = - . 1 ) E [K] + = .LEN DFS (E [K] .r);
 . 17      return E [K] .LEN ;
 18 } // initialize subtree sizes 
. 19  
20 is inline BOOL Check ( int LS, int RS) {
 21 is      BOOL P = . 1 ;
 22 is      IF (! E [LS] .v = E [RS] .v) return  to false ;
 23 is      IF ((E [LS] .L = -! . 1 && E [RS] .r == - . 1 ) || (E [LS] .L == - . 1 ! && E [RS] .r = - . 1 ) || (e [ls] .r = ! -1 && e[rs].l == -1) || (e[ls].r == -1 && e[rs].l != -1)) return false;
24     if(e[ls].l != -1 && e[rs].r != -1) p = p & check(e[ls].l, e[rs].r);
25     if(e[ls].r != -1 && e[rs].l != -1) p = p & check(e[ls].r, e[rs].l);
26     return p;
27 }//判断是否对称 
28     
29 int main(){
30     scanf("%d", &n);
31     for(int i = 1; i <= n; i++) scanf("%d", &e[i].v);
32     for(int i = 1; i <= n; i++) scanf("%d%d", &e[i].l, &e[i].r);
33     dfs(1);
34     for(int i = 1; i <= n; i++)
35         if(check(e[i].l, e[i].r)) ans = max(ans, e[i].len);
36     printf("%d\n", ans);
37     return 0;
38 }
AC Code

 

Guess you like

Origin www.cnblogs.com/New-ljx/p/11827637.html