|BTS建树、层次遍历、完全二叉树检验|7-13 是否完全二叉搜索树 (30分)

    科技2022-08-12  114

    https://blog.csdn.net/wanglinyp/article/details/104967970?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-5.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-5.channel_param

    https://blog.csdn.net/sinat_41144773/article/details/89530403

    https://blog.csdn.net/wanglinyp/article/details/104967970?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-5.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-5.channel_param

    //30 // 8 // 38 24 12 45 58 67 42 51 // 38 45 24 58 42 12 67 51 // NO // 9 // 38 45 42 24 58 30 67 12 51 // 38 45 24 58 42 30 12 67 51 // YES #include <iostream> #include <cstdio> #include <algorithm> #include <queue> using namespace std; struct node{ int data; node* lchild; node* rchild; }; int n; int in[110]; void insert(node* &root,int data){ if(root==NULL){ root=new node; root->data=data; root->lchild=root->rchild=NULL; return; } if(data>root->data)insert(root->lchild,data); else insert(root->rchild,data); } void bfs(node* root){ queue<node*> q; q.push(root); bool flag=true; while(!q.empty()){ node* now=q.front(); q.pop(); if(flag==true){ cout<<now->data; flag=false; } else cout<<" "<<now->data; if(now->lchild!=NULL)q.push(now->lchild); if(now->rchild!=NULL)q.push(now->rchild); } cout<<endl; } bool isCBT(node* root){ queue<node*> q; q.push(root); int num=0; while(q.front()!=NULL){ node* now=q.front(); q.pop(); q.push(now->lchild); q.push(now->rchild); num++; } if(num==n)return true; else return false; } int main() { cin>>n; node* root=NULL; for(int i=0;i<n;i++){ int data; cin>>data; insert(root,data); } bfs(root); bool f=isCBT(root); if(f==true)cout<<"YES"; else cout<<"NO"; return 0; } ----- #include <iostream> #include <cstdio> #include <queue> #include <vector> using namespace std; //vector<int> layerV; int n; struct node{ int data; node* lchild; node* rchild; }; void insert(node* &root,int data){ if(root==NULL){ root=new node(); root->data=data; root->lchild=root->rchild=NULL; return; } if(data>root->data)insert(root->lchild,data); else insert(root->rchild,data); } void layerOrder(node* root){ queue<node*>q; q.push(root); bool flag=true; while(!q.empty()){ node* top = q.front(); q.pop(); //layerV.push_back(top->data); if(flag==true){ cout<<top->data; flag=false; }else{ cout<<" "<<top->data; } if(top->lchild!=NULL)q.push(top->lchild); if(top->rchild!=NULL)q.push(top->rchild); } } bool judge(node* root){ if(root==NULL)return true; queue<node*>q; q.push(root); int sum=0; while(q.front()!=NULL){ node* top = q.front(); q.pop(); //v.push_back(top->data); q.push(top->lchild); q.push(top->rchild); sum++; } if(sum==n)return true; else return false; } int main(){ cin>>n; node* root=NULL; for(int i=0;i<n;i++){ int tmp; cin>>tmp; insert(root,tmp); } layerOrder(root); // for(int i=0;i<layerV.size();i++){ // if(i==0){ // cout<<layerV[i]; // }else{ // cout<<" "<<layerV[i]; // } // } cout<<endl; bool flag=judge(root); if(flag==true){ cout<<"YES"; }else{ cout<<"NO"; } return 0; }
    Processed: 0.020, SQL: 8