1064 Complete Binary Search Tree (30 分)(二叉查找树)

中序遍历建树

#include<bits/stdc++.h>

using namespace std;
const int N=1e3+10;
int s[N];
int n;
int tree[N];
int cnt;
void inorder(int root)
{
    if(root>n) return;
    inorder(root*2);
    tree[root]=s[cnt++];
    inorder(root*2+1);
}
int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++) scanf("%d",&s[i]);
    sort(s,s+n);
    inorder(1);
    for(int i=1;i<=n;i++){
        if(i!=1) printf(" ");
        printf("%d",tree[i]);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/chenchen-12/p/10085233.html