# 二叉树の狂欢

Problem Description

Input

Output

Sample Input
5
1 2 3 4 5
2 3
4 5
- -
- -
- -

Sample Output
OHHHHH HEAP!!!
3

Author
Shoutmon

Source
17浙大考研机试模拟赛

#include <iostream>
#include <vector>
#include <string>
using namespace std;
vector<int> nodeval;
int rts[32],n;

bool isAVL=true;
int ubcounts=0;

bool isCBT=true;
int levcnt[32];

int exchange=0;

int A[32];
struct TreeNode
{
int val;
int left,right;
TreeNode(int v=0,int l=0,int r=0):val(v),left(l),right(r){}
}T[32];
int toindex(string str)
{
if(str[0]=='-') return -1;
return stoi(str);
}
int solve(int r,int i,int depth)
{
if(r==-1) return 0;
int lh=solve(T[r].left,2*i,depth+1);
int rh=solve(T[r].right,2*i+1,depth+1);
if(i>n) isCBT=false;
levcnt[depth]++;
if(i<=n) A[i]=nodeval[r];
if(abs(lh-rh)>=2) isAVL=false,ubcounts++;
return max(lh,rh)+1;
}
{
int temp=A[s];
int parent=s,child;
while(2*parent<=e)
{
child=2*parent;
if(child<e&&A[child]<A[child+1]) ++child;
if(temp>A[child]) break;
A[parent]=A[child];
parent=child;
++exchange;
}
A[parent]=temp;
}
int main()
{
cin>>n;
nodeval.resize(n+1);
for(int i=1;i<=n;++i)
cin>>nodeval[i];
for(int i=1;i<=n;++i)
{
string L,R;
cin>>L>>R;
int left=toindex(L),right=toindex(R);
T[i].left=left;
T[i].right=right;
if(left!=-1)rts[left]=1;
if(right!=-1)rts[right]=1;
}
int root=1;
while(rts[root]==1) ++root;
solve(root,1,1);
if(!isAVL)
{
cout<<"NOT AVL TREE!!!\n"<<ubcounts<<endl;
return 0;
}
if(!isCBT)
{
cout<<"NOT COMPLETE TREE!!!\n";
int lv=1,nodes=1;
while(levcnt[lv]==nodes)
{
nodes*=2;
++lv;
}
cout<<lv-1<<endl;
return 0;
}
cout<<"OHHHHH HEAP!!!\n";
for(int i=n/2;i>=1;--i)
}