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]);
}
}

最新文章

  1. Web Application Penetration Testing Local File Inclusion (LFI) Testing Techniques
  2. android PopupWindow使用实例
  3. ubuntu添加sudo权限
  4. Oracle连接超时
  5. 基础C++ functional
  6. PHP学习(五)----jQuery和JSON数据
  7. 基于GPL329xx linux平台电容屏gsl1680的驱动调试分析
  8. 如何在不装ORACLE的情况下使用PLSQL
  9. JAVA 时间差距,两个时间相差多少天,时,分,秒
  10. phpcms v9为联动菜单字段添加验证提醒功能 解决标题不能为空
  11. toolbar ,textfield,图片拉伸,Bundle
  12. Spring之旅第六篇-事务管理
  13. Vue 自定义一个插件的用法、小案例及在项目中的应用
  14. open()函数 linux中open函数使用
  15. ORA-19566: exceeded limit of 0 corrupt blocks for file E:\xxxx\&lt;datafilename&gt;.ORA.
  16. python bytes类型去除尾部字节
  17. CodeForces834D DP + 线段树
  18. using 自动释放资源示例
  19. JAVA工程师面试常见问题集锦
  20. window10下Docker安装

热门文章

  1. 使用scrapy爬取jian shu文章
  2. Codeforces Round #645 (Div. 2) D、The Best Vacation
  3. Milk Patterns POJ - 3261 后缀数组
  4. linux 部署 .net core mvc
  5. Jenkins 持续集成测试工具
  6. [APUE] 进程控制
  7. C# 类 (6) -继承
  8. JavaScript中的对象引用和复制
  9. js repeatify &amp; no for loop
  10. js jsonParse