PAT甲级 1064 Complete Binary Search Tree (30分)

之前写的还是太复杂了,用中序遍历构建了一个静态二叉树,其实数据顺序对了就行,然后静态从1到n的顺序就是层序遍历,直接输出即可。

#include 
using namespace std;
int n, a[1010], node[1010], countnum = 0;
void inorder(int root)
{
    if(root > n) return;
    inorder(root * 2);
    node[root] = a[countnum++];
    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", node[i]);
        if(i < n) printf(" ");
    }
    return 0;
}

你可能感兴趣的:(PAT甲级题解)