思路:这颗树按照中序遍历,会从小到大,那么将输入的元素排序后,再为每个元素分配对应的下标
import java.util.*;
public class Main {
static void inorder(int i){
if (i>n)return;
inorder(i*2);
arrayList.add(i);
inorder(i*2+1);
}
static ArrayList<Integer>arrayList=new ArrayList<>();
static int n;
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
n=scanner.nextInt();
int a[]=new int[n+1];
StringBuilder sb=new StringBuilder();
for (int i=1;i<=n;i++){
a[i]= scanner.nextInt();
}
Arrays.sort(a);
inorder(1);
int ans[]=new int[n+1];
for (int i=0;i<n;i++){
ans[arrayList.get(i)]=a[i+1];
}
for (int i=1;i<=n;i++){
sb.append(ans[i]).append(" ");
}
System.out.println(sb.toString().trim());
}
}