Binary search tree --A1064.Complete Binary Search Tree (30) constructed entirely binary search tree, using the full binary search tree properties: left child is 2x, right child is 2x + 1

 

 

#include <bits/stdc++.h>
#include <stdio.h>
#include <stdlib.h>
#include <queue>
using namespace std;
const int maxn = 1010;
int temp[maxn],initial[maxn],n;
int ind;
void inorder (int root) {// preorder
    if(root > n){
        return;
    }
    inorder (2 * root); // left child
    temp[root] = initial[ind++];
    inorder (2 * root + 1); // right child node
}
int main () {
    scanf("%d",&n);
    for(int i =0;i<n;++i){
        scanf("%d",&initial[i]);
    }
    sort(initial,initial+n);
    inorder(1);
    for(int i =1;i<=n;++i){
        if(i != n){
            printf("%d ",temp[i]);
        }else{
            printf("%d",temp[i]);
        }
    }
system("pause");
return 0;
}

 

Guess you like

Origin www.cnblogs.com/JasonPeng1/p/12240089.html