PAT甲1064. Complete Binary Search Tree (30)

Complete binary tree, the node sequence is x, the left child sequence is 2x, the right child sequence is 2x+1, and the root node sequence is 1

#include <cstdio>
#include <algorithm>
using namespace std;

int a[1010],CBT[1010],index=0;
int N;

void inorder(int root)
{
    if(root>N)return;
    inorder(root*2);
    CBT[root]=a[index++];
    inorder(root*2+1);
}

int main()  
{   
    scanf("%d",&N);
    for(int i=0;i<N;i++)
    {
        scanf("%d",&a[i]);
    }
    sort(a,a+N);
    inorder(1);
    for(int i=1;i<=N;i++)
    {
        printf("%d",CBT[i]);
        if(i!=N)
            printf(" ");
    }
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325466118&siteId=291194637