2 1 3 2 -1 -1 -1
1
10 2 2 5 5 5 5 4 4 2 3 9 10 -1 -1 -1 -1 -1 -1 -1 -1 -1 2 3 4 5 6 -1 -1 7 8
3
|
满二叉树(深度为 3) |
|
|
完全二叉树(深度为 3) |
完全二叉树(深度为 3) |
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int SIZE=1000005; 4 const int INF=0x3f3f3f3f; 5 #define ll long long 6 7 struct Tree{ 8 int v,Left_Child,Right_Child; 9 }tree[SIZE]; 10 11 void DFS(int L,int R); 12 13 int n,sum=1,ans=1; 14 bool F=true; 15 16 int main() 17 { 18 scanf("%d",&n); 19 for (int i=1;i<=n;i++) scanf("%d",&tree[i].v); 20 for (int i=1;i<=n;i++) scanf("%d%d",&tree[i].Left_Child,&tree[i].Right_Child); 21 22 for (int i=1;i<=n;i++){ 23 F=true; 24 sum=1; 25 DFS(tree[i].Left_Child,tree[i].Right_Child); 26 if (F) ans=max(ans,sum); 27 } 28 29 cout<<ans; 30 31 return 0; 32 } 33 34 void DFS(int L,int R) 35 { 36 if (F==false) return; 37 if (L==-1 && R==-1) return; 38 if ((L==-1 && R!=-1) || (L!=-1 && R==-1)){ 39 F=false; 40 return; 41 } 42 43 if (tree[L].v==tree[R].v){ 44 sum+=2; 45 DFS(tree[L].Left_Child,tree[R].Right_Child); 46 DFS(tree[L].Right_Child,tree[R].Left_Child); 47 } 48 else{ 49 F=false; 50 return; 51 } 52 }
【18NOIP普及组】对称二叉树(信息学奥赛一本通 1981)(洛谷 5018)
原文:https://www.cnblogs.com/ljy-endl/p/11281818.html