用数组
ac代码
#include<cstdio>
#include<cstdlib>
const int maxx = 1005;
int node[maxx];
int tree[maxx];
int pos = 0,n;
int cmp(const void *a,const void *b){
int *pa = (int *)a;
int *pb = (int *)b;
return *pa - *pb;
}
void build(int root)
{
if(root>n)
return;
int lson = root<<1,rson = (root<<1)+1;
build(lson);
tree[root] = node[pos++];
build(rson);
}
int main()
{
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&node[i]);
}
qsort(node,n,sizeof(int),cmp);
build(1);
printf("%d",tree[1]);
for(int i=2;i<=n;i++){
printf(" %d",tree[i]);
}
printf("\n");
return 0;
}