1123. Is It a Complete AVL Tree (30)

来源:互联网 发布:盲僧李小龙皮肤淘宝 编辑:程序博客网 时间:2024/05/17 22:15

1123. Is It a Complete AVL Tree (30)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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.

    

    

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:
588 70 61 63 65
Sample Output 1:
70 63 88 61 65YES
Sample Input 2:
888 70 61 96 120 90 65 68
Sample Output 2:
88 65 96 61 70 90 120 68NO
学了一晚上,终于上午敲出来了。。

#include <cstdio>#include <vector>#include <queue>#include <iostream>using namespace std;struct Node{    struct Node *left,*right;    int val;};int getHeight(struct Node * tree){    if(tree==NULL)        return 0;    int l=getHeight(tree->left);    int r=getHeight(tree->right);    return max(l,r)+1;}struct Node *leftRotate(struct Node *tree){    struct Node * tmp =tree->right;    tree->right=tmp->left;    tmp->left=tree;    return tmp;};struct Node* rightRotate(struct Node *tree){    struct Node *tmp=tree->left;    tree->left=tmp->right;    tmp->right=tree;    return tmp;};struct Node* leftRightRotate(struct Node * tree){    tree->left =leftRotate(tree->left);    tree=rightRotate(tree);    return tree;};struct Node* rightLeftRotate(struct Node *tree){    tree->right=rightRotate(tree->right);    tree=leftRotate(tree);    return tree;};struct Node * Insert( struct Node *tree,int val ){    if(tree==NULL){        tree=new Node();        tree->left=NULL;        tree->right=NULL;        tree->val=val;        return tree;    }    if(tree->val > val ){        tree->left=Insert(tree->left,val);        int l=getHeight(tree->left);        int r=getHeight(tree->right);        if(l-r>=2){            if(tree->left->val  >val )            {                tree=rightRotate(tree);            }else tree=leftRightRotate(tree);        }    }else{        tree->right=Insert(tree->right,val);        int l=getHeight(tree->left);        int r=getHeight(tree->right);        if(r-l>=2)            if(tree->right->val < val)                tree=leftRotate(tree);            else tree=rightLeftRotate(tree);    }    return tree;};int yes=1;void levelOrder(struct Node *tree){    queue<struct Node* > q;    int after=0;    q.push(tree);    vector<int>ans;    while(!q.empty()){        struct Node* tmp=q.front();        q.pop();        ans.push_back(tmp->val);        if( tmp->left ){            q.push(tmp->left);            if(after) yes=0;        }else after=1;        if(tmp->right){            q.push(tmp->right);            if(after) yes=0;        }else after=1;    }    cout<<ans[0];    for(int i=1;i<ans.size();i++)        cout<<" "<<ans[i];    cout<<endl;    if(yes)        cout<<"YES"<<endl;    else cout<<"NO"<<endl;    return ;}int main(){    int n;    cin>>n;    struct Node * tree=NULL;    for(int i=1;i<=n;i++){        int x;        cin>>x;        tree=Insert(tree,x);    }    levelOrder(tree);}




原创粉丝点击