# include<iostream>
# include<vector>
# include<algorithm>
# include<cmath>
using namespace std;
vector<int>a, b;
void solve(int first,int end,int item)
{
if (first > end)
{
return;
}
int N = end - first+1;
int h = log(N + 1) / log(2);
int n1 = N - pow(2, h - 1) ;
int n2 = pow(2, h) - 1;
int nend = min(n1, n2);
b[item] = a[nend+first];
solve(first, first+nend - 1, 2 * item + 1);
solve(first+nend + 1, end, 2 * item + 2);
}
int main(void)
{
int n;
cin >> n;
a.resize(n);
b.resize(n);
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
sort(a.begin(),a.end());
solve(0, n - 1, 0);
printf("%d", b[0]);
for (int i = 1; i < n; i++)
{
printf(" %d", b[i]);
}
return 0;
}
04-树6 Complete Binary Search Tree (30 分)
最新推荐文章于 2024-09-09 12:31:15 发布