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. P2764 最小路径覆盖问题 (最小点覆盖=顶点数-最大匹配)

    题意:最小路径覆盖 题解:对于一个有向图,最小点覆盖 = 顶点数 - 最大匹配 这里的最大匹配指的是将原图中每一个点拆成入点.出点, 每条边连接起点的出点和终点的入点 源点S连接每个点的出点,汇点T连 ...

  2. 【uva 247】Calling Circles(图论--Floyd 传递闭包+并查集 连通分量)

    题意:有N个人互相打了M次电话,请找出所有电话圈(Eg.a→b,b→c,c→d,d→a 就算一个电话圈)并输出.(N≤25,L≤25,注意输出格式) 解法:由于N比较小所有n^2或n^3的复杂度都没有 ...

  3. HDU2837 Calculation(指数循环节)题解

    题意: 已知\(f(0)=1,f(n)=(n\%10)^{f(n/10)}\),求\(f(n)\mod m\) 思路: 由扩展欧拉定理可知:当\(b>=m\)时,\(a^b\equiv a^{b ...

  4. 杭电多校HDU 6656 Kejin Player(概率DP)题解

    题意: 最低等级\(level\ 1\),已知在\(level\ i\)操作一次需花费\(a_i\),有概率\(p_i\)升级到\(level\ i+1\),有\(1 - p_i\)掉级到\(x_i( ...

  5. LVS之DR模式部署

    一.LVS-DR数据包流向分析 为方便进行原理分析,将Client与群集机器放在同一网络中,数据包流经的路线为1-2-3-41.Client 向目标 VIP 发出请求,Director(负载均衡器)接 ...

  6. ES6 Generator vs ES6 async/await

    ES6 Generator vs ES6 async/await next yield promise refs xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只允 ...

  7. TypeScript 4.0 New Features

    TypeScript 4.0 New Features $ npm install typescript@beta https://devblogs.microsoft.com/typescript/ ...

  8. css text gradient color, css fonts gradient color

    css text gradient color, css fonts gradient color css 字体渐变色 demo https://codepen.io/xgqfrms/pen/OJya ...

  9. learning free programming resources form top university of the world

    learning free programming resources form top university of the world Harvard university https://www. ...

  10. Chrome debug & string to object & copy format json

    Chrome debug & string to object & copy format json // save as global variable copy(JSON.stri ...