[c++]代码库
#include <bits/stdc++.h>
using namespace std;
int n;
struct node
{
int data;
int id;
struct node * left, *right;
};
struct node *creat(struct node *root, int x)
{
if(!root)
{
struct node *t;
t= new node;
t->data = x;
t->left = t->right = NULL;
return t;
}
if(root->data < x)
root->left = creat(root->left, x);
else
root->right = creat(root->right, x);
return root;
};
void bfs(struct node *root, int f)
{
queue<node *>q;
root->id = 1;
q.push(root);
int flag = 0;
while(!q.empty())
{
if(flag)
cout<<" ";
struct node *now = q.front();
q.pop();
flag = 1;
if(now->id > n)
f = 1;
cout<<now->data;
if(now->left)
{
now->left->id = now->id<<1;
q.push(now->left);
}
if(now->right)
{
now->right->id = now->id<<1|1;
q.push(now->right);
}
}
cout<<endl;
if(f)
cout<<"NO";
else
cout<<"YES";
}
int main()
{
struct node *root;
root = new node;
root = NULL;
int x, f = 0;
cin>>n;
for(int i = 0; i < n; i++)
{
cin>>x;
root = creat(root,x);
}
bfs(root,f);
return 0;
}
by: 发表于:2017-12-13 10:32:03 顶(0) | 踩(0) 回复
??
回复评论