1064. Complete Binary Search Tree (30)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:

  • The left subtree of a node contains only nodes with keys less than the node's key.
  • The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
  • Both the left and right subtrees must also be binary search trees.

A Complete Binary Tree (CBT) is a tree that is completely filled, with the possible exception of the bottom level, which is filled from left to right.

Now given a sequence of distinct non-negative integer keys, a unique BST can be constructed if it is required that the tree must also be a CBT. You are supposed to output the level order traversal sequence of this BST.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (<=1000). Then N distinct non-negative integer keys are given in the next line. All the numbers in a line are separated by a space and are no greater than 2000.

Output Specification:

For each test case, print in one line the level order traversal sequence of the corresponding complete binary search tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.

Sample Input:

  1. 10
  2. 1 2 3 4 5 6 7 8 9 0

Sample Output:

  1. 6 3 8 1 5 7 9 0 2 4
  2.  
  3. 先按照定义构建完全二叉树的框架,接下来就是把数字分别填到这颗二叉树的节点中,使得其满足二叉搜索树的定义。其实填入节点可以直接中序遍历这颗二叉树框架即可。
    AC代码:
  1. #define _CRT_SECURE_NO_DEPRECATE
  2. #pragma warning(disable:4996)
  3. #include<iostream>
  4. #include<string>
  5. #include<algorithm>
  6. #include<map>
  7. #include<cctype>
  8. #include<cmath>
  9. #include<cstring>
  10. #include<vector>
  11. #include<set>
  12. #include<queue>
  13. #include<limits.h>
  14. using namespace std;
  15. typedef long long ll;
  16. #define N_MAX 1000+5
  17. #define INF 0x3f3f3f3f
  18. int n, a[N_MAX];
  19. struct Node {
  20. int key;
  21. int l_child, r_child;
  22. Node(int key=INF,int l_child=INF,int r_child=INF):key(key),l_child(l_child),r_child(r_child) {}
  23. }node[N_MAX];
  24. void init(int n) {//建立完全二叉树的框架
  25. n--;
  26. int cur = ;
  27. int flag = ;
  28. while (n--) {
  29. if (!(flag & ))node[cur].l_child = * cur + ;
  30. else {
  31. node[cur].r_child = *cur+;
  32. cur++;
  33. }
  34. flag++;
  35. }
  36. }
  37.  
  38. int cnt = ;
  39. void inorder(int n) {
  40. if(node[n].l_child!=INF)inorder(node[n].l_child);
  41. node[n].key = a[cnt++];
  42. if(node[n].r_child!=INF)inorder(node[n].r_child);
  43. }
  44. vector<int>vec;
  45. void bfs() {
  46. queue<int>que;
  47. que.push();
  48. while (!que.empty()) {
  49. int p = que.front(); que.pop();
  50. vec.push_back(node[p].key);
  51. if (node[p].l_child != INF)que.push(node[p].l_child);
  52. if (node[p].r_child != INF)que.push(node[p].r_child);
  53. }
  54. }
  55. int main() {
  56. while (scanf("%d", &n) != EOF) {
  57. for (int i = ; i < n; i++) {
  58. scanf("%d", &a[i]);
  59. }
  60. sort(a,a+n);
  61. init(n);
  62. cnt = ;
  63. inorder();
  64. bfs();
  65. for (int i = ; i < vec.size();i++) {
  66. printf("%d%c",vec[i],i+==vec.size()?'\n':' ');
  67. }
  68. }
  69. return ;
  70. }

pat 甲级 1064. Complete Binary Search Tree (30)的更多相关文章

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

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

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

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

  3. PAT 甲级 1064 Complete Binary Search Tree

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

  4. 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 ...

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

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

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

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

  7. 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 ...

  8. PAT甲级——A1064 Complete Binary Search Tree

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

  9. 1064 Complete Binary Search Tree (30分)(已知中序输出层序遍历)

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

随机推荐

  1. 1503: [NOI2004]郁闷的出纳员

    Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 13723  Solved: 4989[Submit][Status][Discuss] Descripti ...

  2. 多页应用 Webpack4 配置优化与踩坑记录

    前言 最近新起了一个多页项目,之前都未使用 webpack4 ,于是准备上手实践一下.这篇文章主要就是一些配置介绍,对于正准备使用 webpack4 的同学,可以做一些参考. webpack4 相比之 ...

  3. nginx的缓存服务

    都知道缓存的目的是为了减小服务端的压力,可以在客户端直接取到数据 客户端---------------nginx(代理缓存)------------------服务端 代理缓存的描述: 就是客户端发送 ...

  4. HDU:2594-Simpsons’ Hidden Talents

    Simpsons' Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...

  5. 初识Java之入门学习(扫盲)

    一,开发环境的配置 1. jdk1.8的安装 2. 环境变量的配置 3.MyEclipse8.5的安装 jdk是什么: JDK 是Java开发工具包 (Java Development Kit ) 的 ...

  6. sql中一个服务器建立另一个服务器的连接

    EXEC sp_addlinkedserver 'TonyLink','','SQLOLEDB','111.111.1.111(服务器名)' EXEC sp_addlinkedsrvlogin 'To ...

  7. windows server 2008解决无法PING通问题

    今天安装服务器(server 2008),配置完IP地址后,发现局域网其它电脑无法PING通服务器,测线仪测试链路都正常,网线接别的电脑也正常,以为是网卡问题,于是ping了自己的IP,发现能PING ...

  8. 爬虫工程师常用的 Chrome 插件

    做多了爬虫都知道,写一个爬虫大部分时间不是在代码上,而是在分析网页上,所有有一套好用的工具可以极大节省劳动力,这里把平时积累的一些 Chrome 插件分享出来,均来自本人和同事推荐,并不定时更新,欢迎 ...

  9. SetUnhandledExceptionFilter

    SetUnhandledExceptionFilter 设置未捕获异常处理 通常windows程序长时间运行,会发生各种问题,例如访问异常,内存溢出,堆栈破坏等. 这时候通常希望程序自己能增加处理,而 ...

  10. 手机APP设计网

     http://hao.xueui.cn/ http://www.25xt.com/