Skip to content

Instantly share code, notes, and snippets.

@lishunan246
Created January 2, 2016 08:13
Show Gist options
  • Save lishunan246/896ea14ac4cabf2ece1a to your computer and use it in GitHub Desktop.
Save lishunan246/896ea14ac4cabf2ece1a to your computer and use it in GitHub Desktop.
1064. Complete Binary Search Tree
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int N;
int pos = 0;
int tree[1005];
vector<int> input;
void build(int root)
{
if (root > N)
return;
auto l = 2 * root, r = 2 * root + 1;
build(l);
tree[root] = input[pos++];
build(r);
}
int main()
{
cin >> N;
for (auto i = 0; i < N; ++i)
{
int t;
cin >> t;
input.push_back(t);
}
sort(input.begin(), input.end());
build(1);
cout << tree[1];
for (auto i = 2; i <= N; ++i)
cout << " " << tree[i];
cout << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment