-
Notifications
You must be signed in to change notification settings - Fork 814
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update 1064. Complete Binary Search Tree (30).cpp
- Loading branch information
Showing
1 changed file
with
10 additions
and
19 deletions.
There are no files selected for viewing
29 changes: 10 additions & 19 deletions
29
AdvancedLevel_C++/1064. Complete Binary Search Tree (30).cpp
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,21 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <algorithm> | ||
#include <cmath> | ||
using namespace std; | ||
vector<int> in, level; | ||
void levelorder(int start, int end, int index) { | ||
if(start > end) return ; | ||
int n = end - start + 1; | ||
int l = log(n + 1) / log(2); // 除了最后一层的层数 | ||
int leave = n - (pow(2, l) - 1);// 最后一层的叶子节点数 | ||
int root = start + (pow(2, l - 1) - 1) + min((int)pow(2, l - 1), leave); // pow(2, l - 1) - 1是除了root结点所在层和最后一层外,左子树的结点个数,pow(2, l - 1) 是l+1层最多拥有的属于根结点左子树的结点个数,min(pow(2, l - 1), leave)是最后一个结点真正拥有的属于根结点左子树上的结点个数 | ||
level[index] = in[root]; | ||
levelorder(start, root - 1, 2 * index + 1); | ||
levelorder(root + 1, end, 2 * index + 2); | ||
int in[1010], level[1010], n, t = 0; | ||
void inOrder(int root) { | ||
if (root >= n) return ; | ||
inOrder(root * 2 + 1); | ||
level[root] = in[t++]; | ||
inOrder(root * 2 + 2); | ||
} | ||
int main() { | ||
int n; | ||
scanf("%d", &n); | ||
in.resize(n); | ||
level.resize(n); | ||
for(int i = 0 ; i < n; i++) | ||
for (int i = 0; i < n; i++) | ||
scanf("%d", &in[i]); | ||
sort(in.begin(), in.end()); | ||
levelorder(0, n - 1, 0); | ||
sort(in, in + n); | ||
inOrder(0); | ||
printf("%d", level[0]); | ||
for(int i = 1; i < n; i++) | ||
for (int i = 1; i < n; i++) | ||
printf(" %d", level[i]); | ||
return 0; | ||
} |