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 (≤). 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
  1. #include<cstdio>
  2. #include<algorithm>
  3. using namespace std;
  4. const int maxn = ;
  5.  
  6. int num[maxn];
  7. int CBT[maxn];
  8. int index = ;
  9.  
  10. void inOrder(int root, int n);
  11.  
  12. int main()
  13. {
  14. int n;
  15. scanf("%d",&n);
  16. for (int i = ; i < n; i++)
  17. {
  18. scanf("%d",&num[i]);
  19. }
  20. sort(num,num+n);
  21.  
  22. inOrder(,n);
  23. for (int i = ; i <= n; i++)
  24. {
  25. printf("%d",CBT[i]);
  26. if (i < n)
  27. {
  28. printf(" ");
  29. }
  30. }
  31.  
  32. return ;
  33. }
  34.  
  35. void inOrder(int root, int n)
  36. {
  37. if (root > n)
  38. {
  39. return ;
  40. }
  41.  
  42. inOrder(root*, n);
  43. CBT[root] = num[index++];
  44. inOrder(root*+, n);
  45. }

04-树6 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 (30分)

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

  3. PTA 04-树6 Complete Binary Search Tree (30分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/669 5-7 Complete Binary Search Tree   (30分) A ...

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

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

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

    题意:输入一个正整数N(<=1000),接着输入N个非负整数(<=2000),输出完全二叉树的层次遍历. AAAAAccepted code: #define HAVE_STRUCT_TI ...

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

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

  7. pat04-树8. Complete Binary Search Tree (30)

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

  8. pat1064. Complete Binary Search Tree (30)

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

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

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

随机推荐

  1. vs2017专业版和企业版的密钥

    Enterprise: NJVYC-BMHX2-G77MM-4XJMR-6Q8QF Professional: KBJFW-NXHK6-W4WJM-CRMQB-G3CDH

  2. 好用的数据库字典查看工具SQLToolbelt

    工作中经常为诸多的陌生或没有任何表或者字段说明或者文档庞大数据库和数据库表所烦恼,有以下场景: 1.新进入一家公司,开始接触新的项目,领导给你一大堆文档,在不了解具体逻辑的情况下,除了项目的结构,能让 ...

  3. SQLServer作业调用链接服务器失败解决办法

    新建一个SQL作业,语句手动执行OK,但是作业计划执行总是报错. 消息已以用户 NT SERVICE\SQLSERVERAGENT 的身份执行. 链接服务器 "172.16.10.23&qu ...

  4. gcc 编译过程

    gcc 编译过程从 hello.c 到 hello(或 a.out)文件, 必须历经 hello.i. hello.s. hello.o,最后才得到 hello(或a.out)文件,分别对应着预处理. ...

  5. 2019 头条java面试笔试总结 (含面试题解析)

       本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条等公司offer,岗位是Java后端开发,因为发展原因最终选择去了头条,入职一年时间了,也成为了面试官,之前面 ...

  6. spark存储管理之磁盘存储--DiskStore

    DiskStore 接着上一篇,本篇,我们分析一下实现磁盘存储的功能类DiskStore,这个类相对简单.在正式展开之前,我觉得有必要大概分析一下BlockManager的背景,或者说它的运行环境,运 ...

  7. Beego学习笔记四:编写Model

    MVC实践一:编写模型 1>     打开mysql数据库,设计表的结构 <1>登录mysql数据库,如下 <2>这三个标注的参数皆有用,需要谨记. <3>创 ...

  8. Mysql 游标初识

    MySql 游标初识 认识 游标(cursor), 按字面意思可理解为, 游动的标识, 或者叫做"光标", 这样更容易理解. 就好比现有一张表存储了n行记录, 然后我想每次取出一行 ...

  9. Prometheus学习笔记(5)Grafana可视化展示

    目录 一.Grafana安装和启动 二.配置数据源 三.配置dashboard 四.配置grafana告警 一.Grafana安装和启动 Grafana支持查询Prometheus.从Grafana ...

  10. PAT甲级1018留坑——第一个测试点未过(Dijikstar+Dfs)

    题目分析: 主要是先进行狄杰斯特拉求出0点到每个点的最短路后用dfs求出所有的路径,将路径方案加入vector排序选择need最小和rest最小的方案,但是第一个测试却过不去,欢迎指正!!感谢!! 值 ...