根据节点的编号判断是否是完全二叉搜索树,如果最大的节点编号不等于n就不是,否则就是。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#define ll long long
#define lowbit(x) ((~x+1)&x)
#define inf 0x3f3f3f3f
using namespace std;
struct node {
struct node *l,*r;
int val;
};
int n,m,num;
struct node *build(struct node *root,int w,int in) {
if(root==NULL) {
root = (struct node *)malloc(sizeof(struct node));
root->val = w;
root->l = NULL;
root->r = NULL;
num = max(num,in);
return root;
}
if(root->val > w) root->r=build(root->r,w,in<<1|1);
else root->l = build(root->l,w,in<<1);
return root;
}
void bfs(struct node *root) {
queue<node*>q;
q.push(root);
cout<<root->val;
while(!q.empty()) {
struct node *top;
top=q.front();
q.pop();
if(top->l) {
cout<<' '<<top->l->val;
q.push(top->l);
}
if(top->r) {
cout<<' '<<top->r->val;
q.push(top->r);
}
}
}
int main() {
cin>>n;
struct node *root;
root = (struct node *)malloc(sizeof(struct node));
root=NULL;
for(int i=1; i<=n; i++) {
int k;
cin>>k;
root = build(root,k,1);
}
bfs(root);
cout<<endl;
if(num==n) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
return 0;
}