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

PAT-1064(Complete Binary Search Tree)JAVA实现的更多相关文章

  1. PAT 1064 Complete Binary Search Tree[二叉树][难]

    1064 Complete Binary Search Tree (30)(30 分) A Binary Search Tree (BST) is recursively defined as a b ...

  2. PAT 1064 Complete Binary Search Tree

    #include <iostream> #include <cstdio> #include <cstdlib> #include <vector> # ...

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

    PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...

  4. PAT题库-1064. Complete Binary Search Tree (30)

    1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...

  5. pat 甲级 1064. Complete Binary Search Tree (30)

    1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...

  6. 1064. Complete Binary Search Tree (30)【二叉树】——PAT (Advanced Level) Practise

    题目信息 1064. Complete Binary Search Tree (30) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B A Binary Search Tr ...

  7. PAT 甲级 1064 Complete Binary Search Tree (30 分)(不会做,重点复习,模拟中序遍历)

    1064 Complete Binary Search Tree (30 分)   A Binary Search Tree (BST) is recursively defined as a bin ...

  8. pat 甲级 1064 ( Complete Binary Search Tree ) (数据结构)

    1064 Complete Binary Search Tree (30 分) A Binary Search Tree (BST) is recursively defined as a binar ...

  9. PAT 甲级 1064 Complete Binary Search Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/994805407749357568 A Binary Search Tree ( ...

  10. PAT Advanced 1064 Complete Binary Search Tree (30) [⼆叉查找树BST]

    题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...

随机推荐

  1. P1020 导弹拦截(LIS)

    题目描述 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度.某天,雷达捕捉到敌国的导弹 ...

  2. Complete the sequence! POJ - 1398 差分方法找数列规律

    参考链接:http://rchardx.is-programmer.com/posts/16142.html vj题目链接:https://vjudge.net/contest/273000#stat ...

  3. 单源最短路问题 Dijkstra 算法(朴素+堆)

    选择某一个点开始,每次去找这个点的最短边,然后再从这个开始不断迭代,更新距离. 代码: 朴素(vector存图) #include <iostream> #include <cstd ...

  4. 六、Python集合定义和基本操作方法

    一.集合的定义方法及特点 1.特点: (1)由不同元素组成 #集合由不同元素构成 s={1,2,3,3,4,3,3,} print(s)#运行结果:{1, 2, 3, 4} (2)集合无序 #集合无序 ...

  5. HTTP笔记2--简单的HTTP 协议

    HTTP概念 HTTP用于客户端和服务器之间的通信 客户端:请求访问文本或图像等资源的一端 服务器端而提供资源响应的一端 通过请求和响应的交换达成通信 HTTP 协议规定,请求从客户端发出,最后服务器 ...

  6. C语言之库函数的模拟与使用

    C语言之库函数的模拟与使用 在我们学习C语言的过程中,难免会遇到这样的一种情况: 我们通常实现一个功能的时候,费尽心血的写出来,却有着满满的错,这时却有人来告诉你说:这个功能可以用相应的库函数来实现. ...

  7. Redis 数据迁移 & 数据审计

    Redis 数据迁移 安装迁移工具 # 安装依赖 [root@dbtest03 ~]# yum install -y automake libtool autoconf bzip2 git # 拉取工 ...

  8. kubernetes进阶(二)核心网络插件Flannel

    网络插件Flannel介绍:https://www.kubernetes.org.cn/3682.html 首先,flannel利用Kubernetes API或者etcd用于存储整个集群的网络配置, ...

  9. spring再学习之注解

    1.使用注解配置spring <?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi= ...

  10. vue & this.$router.resolve

    vue & this.$router.resolve gotoAutoUpdate (query = {}) { const { href } = this.$router.resolve({ ...