编程语言
首页 > 编程语言> > PAT-1064(Complete Binary Search Tree)JAVA实现

PAT-1064(Complete Binary Search Tree)JAVA实现

作者:互联网

Complete Binary Search Tree

PAT-1064

/**
 * @Author WaleGarrett
 * @Date 2020/9/5 8:20
 */

import java.util.Arrays;
import java.util.Scanner;

/**
 * PAT-1043,1064,1066,1086,1089,1099,1098
 * 7
 * 8 6 5 7 10 8 11
 */
public class PAT_1064 {
    static final int maxn=1003;
    static int[] node;
    static int[] tree;//存储二叉树
    static int N;
    static int position=0;
    static void inOrder(int root){
        if(root>N)
            return;
        inOrder(root<<1);
        tree[root]=node[position++];
        inOrder(root<<1|1);
    }
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        int n=scanner.nextInt();
        N=n;
        node=new int[n];
        tree=new int[n+1];
        int i=0;
        while(n!=0){
            node[i]=scanner.nextInt();
            i++;
            n--;
        }
        Arrays.sort(node);
        inOrder(1);
        for(i=1;i<N;i++){
            System.out.print(tree[i]+" ");
        }
        System.out.println(tree[N]);
    }
}

标签:Binary,Search,遍历,PAT,int,1064,二叉,static
来源: https://www.cnblogs.com/GarrettWale/p/13617358.html