Created
January 2, 2016 08:13
-
-
Save lishunan246/896ea14ac4cabf2ece1a to your computer and use it in GitHub Desktop.
1064. Complete Binary Search Tree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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