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:

    10
    1 2 3 4 5 6 7 8 9 0

    Sample Output:

    6 3 8 1 5 7 9 0 2 4
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    const int maxn = ; int n;
    int index = ;
    int CBT[maxn],num[maxn]; void inOrder(int root){
    if(root > n) return;
    inOrder(root*);
    CBT[root] = num[index++];
    inOrder(root*+);
    } int main(){ scanf("%d",&n);
    for(int i = ; i < n; i++){
    scanf("%d",&num[i]);
    }
    sort(num,num+n);
    inOrder();
    for(int i = ; i <= n; i++){
    printf("%d",CBT[i]);
    if(i < n) printf(" ");
    }
    return ;
    }

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. 04-树6 Complete Binary Search Tree (30 分)

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

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

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

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

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

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

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

  9. pat1064. Complete Binary Search Tree (30)

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

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

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

随机推荐

  1. ADO.NET DataTable的复制(clone)

    using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=test;Integrated Se ...

  2. Java解析XML文档——dom解析xml

    一.前言 用Java解析XML文档,最常用的有两种方法:使用基于事件的XML简单API(Simple API for XML)称为SAX和基于树和节点的文档对象模型(Document Object M ...

  3. [.NET] GC垃圾回收机制

    前言: 在.NET程序开发中,为了将开发人员从繁琐的内存管理中解脱出来,将更多的精力花费在业务逻辑上,CLR提供了自动执行垃圾回收的机制来进行内存管理.开发人员甚至感觉不到这一过程的存在.CLR执行垃 ...

  4. WebClient使用与IIS7最大上传文件--升级&引导窗口&目录同步完整解决方法

    IIS7最大上传文件说明:http://www.mzwu.com/article.asp?id=2449 WebClient使用说明使用using  及时回收资源 using(var wc=new W ...

  5. redis集群种类(转)

    原文:http://blog.csdn.net/c295477887/article/details/52487621 关于redis主从.哨兵.集群的介绍网上很多,这里就不赘述了. 一.主从 通过持 ...

  6. thinkphp整合系列之phpexcel导入excel数据

    一:导入phpexcel /ThinkPHP/Library/Vendor/PHPExcel 二:导入excel的函数 /** * 导入excel文件 * @param string $file ex ...

  7. Project Tango Explorer

    https://sensortower.com/android/ie/projecttango-google/app/project-tango-explorer/com.projecttango.t ...

  8. POJ2349 Arctic Network 2017-04-13 20:44 40人阅读 评论(0) 收藏

    Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19113   Accepted: 6023 D ...

  9. SqlServer循环执行存储过程

    begin --申明变量 ) declare @zycs int --赋值变量 --申明游标 declare order_cursor cursor for (select blh, zycs fro ...

  10. Always on (HA 负载均衡 异地容灾 一体化 )

    Sqlserver 2012 开始,以往困扰我们的三个棘手问题:可扩展性.数据保护.异地容灾可以统一通过alwayson 来实现.2014  支持secondary 节点更是达到8个.在硬件调配方面比 ...