#include<bits/stdc++.h>
using namespace std;
const int MAXN=100010;
struct Node{
int data;
Node* left;
Node* right;
Node(){
left=NULL;
right=NULL;
}
}node[MAXN];
vector<int> in,le,pre;
Node* build(int l,int r){
Node* root=new Node;
int num=r-l+1;int level=0;
level=(int)(log(num*1.0)/log(2.0))+1;
int numleft,numright;
if(level==1) numleft=0;
else{
if(3*pow(2.0,level-2)-1>=num){
numright = pow(2.0,level-2)-1;
numleft = num-1-numright;
}
else{
numleft = pow(2.0,level-1)-1;
numright = num-1-numleft;
}
}
root->data=in[l+numleft];
if(numleft==0){
root->left=NULL;
}else{
root->left=build(l,l+numleft-1);
}
if(num-numleft-1==0){
root->right=NULL;
}else{
root->right=build(r-numright+1,r);
}
return root;
}
void levetra(Node* root){
queue<Node*> q;
q.push(root);
while(!q.empty()){
Node* now=q.front();
q.pop();
le.push_back(now->data);
if(now->left!=NULL) q.push(now->left);
if(now->right!=NULL) q.push(now->right);
}
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("in.txt","r",stdin);
#endif
int n;cin>>n;in.resize(n);
for(int i=0;i<n;i++){
cin>>in[i];
}
sort(in.begin(),in.end());
Node* root=build(0,n-1);
levetra(root);
for(int i=0;i<n;i++){
if(i==0) cout<<le[i];
else cout<<' '<<le[i];
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
const int MAXN=100010;
struct Node{
int data;
Node* left;
Node* right;
Node(){
data=-1;
left=NULL;
right=NULL;
}
}node[MAXN];
vector<int> in,le;
Node* build(int l,int r){
if(l>r) return NULL;
int num=r-l+1;int level=0;
/*for(int i=1;i<num;i++){
if(num<=(pow(2,i)-1)&&num>(pow(2,i-1)-1)){
level=i;break;
}
}*/
level=log(num)/log(2)+1;
int numleft;
if(level==1){
numleft=0;
}else{
int halflastlevelnum=pow(2,level-2);
int lastlevelnum=num-(pow(2,level-1)-1);
if(lastlevelnum<=halflastlevelnum)
numleft=lastlevelnum+(pow(2,level-2)-1);
else{
numleft=halflastlevelnum+(pow(2,level-2)-1);
}
}
Node* root=new Node;
root->data=in[l+numleft];
root->left=build(l,l+numleft-1);
root->right=build(l+numleft+1,r);
return root;
}
void levetra(Node* root){
queue<Node*> q;
q.push(root);
while(!q.empty()){
Node* now=q.front();
q.pop();
le.push_back(now->data);
if(now->left!=NULL) q.push(now->left);
if(now->right!=NULL) q.push(now->right);
}
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("in.txt","r",stdin);
#endif
int n;cin>>n;in.resize(n);
for(int i=0;i<n;i++){
cin>>in[i];
}
sort(in.begin(),in.end());
Node* root=build(0,n-1);
levetra(root);
for(int i=0;i<n;i++){
if(i==0) cout<<le[i];
else cout<<' '<<le[i];
}
return 0;
}
两段代码都能AC,有一个问题卡了半天,就是计算层数的时候本来想用for循环,结果总是有两个节点的位置不太对,后来只能妥协用公式,可以AC了
#include<bits/stdc++.h>
using namespace std;
const int MAXN=100010;
struct Node{
int data;
Node* left;
Node* right;
Node(){
data=-1;
left=NULL;
right=NULL;
}
}node[MAXN];
vector<int> in,le;
Node* build(int l,int r){
if(l>r) return NULL;
int num=r-l+1;int level=0;
/*for(int i=1;i<num;i++){
if(num<=pow(2,i)&&num>pow(2,i-1)){
level=i;break;
}
}*/
level=log(num)/log(2)+1;
int numleft;
int halflastlevelnum=pow(2,level-1)/2;
int lastlevelnum=num-(pow(2,level-1)-1);
if(lastlevelnum<=halflastlevelnum)
numleft=lastlevelnum+(pow(2,level-1)-2)/2;
else{
numleft=halflastlevelnum+(pow(2,level-1)-2)/2;
}
Node* root=new Node;
root->data=in[l+numleft];
root->left=build(l,l+numleft-1);
root->right=build(l+numleft+1,r);
return root;
}
void levetra(Node* root){
queue<Node*> q;
q.push(root);
while(!q.empty()){
Node* now=q.front();
q.pop();
le.push_back(now->data);
if(now->left!=NULL) q.push(now->left);
if(now->right!=NULL) q.push(now->right);
}
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("in.txt","r",stdin);freopen("out.txt","w",stdout);
#endif
int n;cin>>n;in.resize(n);
for(int i=0;i<n;i++){
cin>>in[i];
}
sort(in.begin(),in.end());
Node* root=build(0,n-1);
levetra(root);
for(int i=0;i<n;i++){
if(i==0) cout<<le[i];
else cout<<' '<<le[i];
}
return 0;
}