团体程序设计天体赛(L3-010 是否完全二叉搜索树 (30 分))

题目:

思路分析:

可以通过数组来模拟建树过程

也可以‘真实‘建树

判断是否为完全二叉树 只需要判断点的个数是否==n

代码实现:

const int MAX=1000010;
struct node{
    node *l,*r;
    int data;
};
int n;
node *insert(node *root,int x){
    if(root==NULL){
        root=new node;
        root->data=x;
        root->l=root->r=NULL;
        return root;
    }
    else{
        if(root->data>x){
            root->r=insert(root->r,x);
        }
        else root->l=insert(root->l,x);
    }
    return root;
}
void print_ch(node *root){
    queue<node*>qu;
    
    if(root){
        cout<<root->data;
        qu.push(root);
    }
    while (!qu.empty()) {
        node *now=qu.front();
        qu.pop();
        if(now->l){
            cout<<" "<<now->l->data;
            qu.push(now->l);
        }
        if(now->r){
            cout<<" "<<now->r->data;
            qu.push(now->r);
        }
    }
}
void  check(node *root){
    queue<node*>qu;
    qu.push(root);
    int num=0;
    while (qu.front()!=NULL) {
        node *now=qu.front();
        qu.pop();
        qu.push(now->l);
        qu.push(now->r);
        num++;
    }
    if(n==num)cout<<"YES";
    else cout<<"NO";
}
int main(){
    cin>>n;
    node *root=NULL;
    for(int i=0;i<n;i++){
        int x;
        cin>>x;
        root=insert(root,x);
    }
    print_ch(root);
    cout<<endl;
    check(root);
}
/*
*@Author:   GuoJinlong
*@Language: C++
*/
//#include <bits/stdc++.h>
/*
 *                                                     __----~~~~~~~~~~~------___
 *                                    .  .   ~~//====......          __--~ ~~
 *                    -.            \_|//     |||\\  ~~~~~~::::... /~
 *                 ___-==_       _-~o~  \/    |||  \\            _/~~-
 *         __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
 *     _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
 *   .~       .~       |   \\ -_    /  /-   /   ||      \   /
 *  /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
 *  |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
 *           '         ~-|      /|    |-~\~~       __--~~
 *                       |-~~-_/ |    |   ~\_   _-~            /\
 *                            /  \     \__   \/~                \__
 *                        _--~ _/ | .-~~____--~-/                  ~~==.
 *                       ((->/~   '.|||' -_|    ~~-/ ,              . _||
 *                                  -_     ~\      ~~---l__i__i__i--~~_/
 *                                  _-~-__   ~)  \--______________--~~
 *                                //.-~~~-~_--~- |-------~~~~~~~~
 *                                       //.-~~~--\
 *                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 *                               神兽保佑            永无BUG
 */


const int MAX=10001;
int a[MAX];
void  built(int pos,int x){
    if(a[pos]==-1){
        a[pos]=x;
        return;
    }
    if(x>a[pos]){
        built(pos*2,x);
    }
    else{
        built(pos*2+1,x);
    }

}
int main(){
    int n;
    cin>>n;
    mms(a,-1);
    for(int i=0;i<n;i++){
        int x;
        cin>>x;
        built(1,x);
    }
    int pos=1;
    int t=0;
    while (t<n) {
        while (a[pos]==-1) {
            pos++;
        }
        if(pos==1) cout<<a[pos++];
        else cout<<" "<<a[pos++];
        t++;
    }

    cout<<endl;
    if(n+1==pos){
        cout<<"YES";
    }
    else cout<<"NO"<<endl;

}

猜你喜欢

转载自blog.csdn.net/m0_57006708/article/details/121241407