1123 Is It a Complete AVL Tree (30分)

An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. Figures 1-4 illustrate the rotation rules.

F1.jpg F2.jpg F3.jpg F4.jpg Now given a sequence of insertions, you are supposed to output the level-order traversal sequence of the resulting AVL tree, and to tell if it is a complete binary tree.

Input Specification: Each input file contains one test case. For each case, the first line contains a positive integer N (≤ 20). Then N distinct integer keys are given in the next line. All the numbers in a line are separated by a space.

Output Specification:
For each test case, insert the keys one by one into an initially empty AVL tree. Then first print in a line the level-order traversal sequence of the resulting AVL tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line. Then in the next line, print YES if the tree is complete, or NO if not.

Sample Input 1:

5
88 70 61 63 65

Sample Output 1:
70 63 88 61 65 YES

Sample Input 2:

8
88 70 61 96 120 90 65 68

Sample Output 2:

88 65 96 61 70 90 120 68
NO

#include<bits/stdc++.h>
using namespace std;
struct node{
    node *left,*right;
    int val;
};
vector<int>pre,cpt_In,avl_In;
int N,val,ok=0;
node *rotateR(node *root){
    node *temp=root->left;
    root->left=temp->right;
    temp->right=root;
    return temp;
}
node *rotateL(node *root){
    node *temp=root->right;
    root->right=temp->left;
    temp->left=root;
    return temp;
}
node *rotateLR(node *root){
    root->left = rotateL(root->left);
    return rotateR(root);
}
node *rotateRL(node *root){
    root->right = rotateR(root->right);
    return rotateL(root);
}
int getheight(node *root){
    if(root==NULL)return 0;
    return max(getheight(root->left),getheight(root->right))+1;
}
node *Insert(node *root,int val){
    if(root==NULL){ 
        root = new node;
        root->val = val;
        root->left = root->right = NULL;
    }
    else if(val<root->val){
        root->left = Insert(root->left,val);
        if(getheight(root->left)-getheight(root->right)==2)
            root = val<root->left->val?rotateR(root):rotateLR(root); 
    }
    else{
        root->right = Insert(root->right,val);
        if(getheight(root->left)-getheight(root->right)==-2)
            root = val>root->right->val?rotateL(root):rotateRL(root); 
    }
    return root;
}

void AVLIn(node *root){
    if(root==NULL)return;
    AVLIn(root->left);
    avl_In.push_back(root->val);
    AVLIn(root->right);
}
void CptInOrder(int root){
    if(root>N)return;
    CptInOrder(2*root);
    cpt_In.push_back(pre[root]);
    CptInOrder(2*root+1);
}
void Levelorder(node *root){
    queue<node* >q;
    q.push(root);
    while(!q.empty()){
        root=q.front();
        q.pop();
        pre.push_back(root->val);
        if(ok++)cout<<' ';
        cout<<root->val;
        if(root->left!=NULL)    q.push(root->left);
        if(root->right!=NULL)   q.push(root->right);
    }
}
int main(){
    node *root=NULL;
    pre.push_back(0);
    cin>>N;
    for(int i=0;i<N;i++){
        scanf("%d",&val);
        root = Insert(root,val);
    }
    Levelorder(root);
    AVLIn(root);
    CptInOrder(1);
    printf("\n%s",avl_In==cpt_In?"YES":"NO");
    return 0;
}