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

题目分析:刚开始我想的是先把末尾的多余的元素 计入计算根节点的位置 但欠缺考虑到对于k层树来说
若第k层元素大于2的k-1次 我这样的做法就出了问题 只过了2个点(21分) //给分真高
 #define _CRT_SECURE_NO_WARNINGS
#include <climits>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
int Array[];
int Queue[];
void EnQueue(int begin, int end,int pos) //左闭右开
{
if (begin >= end)
return;
Queue[pos] = Array[(begin + end) / ];
EnQueue(begin, (begin + end)/, * pos + );
EnQueue((begin + end) / + , end, * pos + );
}
int main()
{
int N;
cin >> N;
for (int i = ; i < N; i++)
cin >> Array[i];
sort(Array, Array + N);
int sum = ;
for (; sum * < N; sum *= );
int offset = (N - sum)/;
int i = ;
Queue[i] = Array[N / + offset]; //根节点入队
//分别递归处理左右
EnQueue(, N / + offset, * i + );
EnQueue(N / + offset + ,N, * i + );
for (int i = ; i < N - ; i++)
cout << Queue[i] << " ";
if(N!=)
cout << Queue[N- ];
return ;
}

参考别人的做法
 #define _CRT_SECURE_NO_WARNINGS
#include <climits>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
int Array[];
int Queue[];
int N;
int id;
void EnQueue(int root) //左闭右开
{
if (root >= N)
return;
EnQueue( * root + );
Queue[root] = Array[id++];
EnQueue( * root + );
}
int main()
{
cin >> N;
for (int i = ; i < N; i++)
cin >> Array[i];
sort(Array, Array + N);
EnQueue();
for (int i = ; i < N - ; i++)
cout << Queue[i] << " ";
cout << Queue[N - ];
return ;
}

来自https://blog.csdn.net/feng_zhiyu/article/details/82219702

果然 很多代码真的是又短又好 虽然原理简单 但体现出的思想正是让我感到震撼

 

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

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

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

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

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

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

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

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

随机推荐

  1. 基于正向扫描的并行区间连接平面扫描算法(IEEE论文)

    作者: Panagiotis Bouros ∗Department of Computer ScienceAarhus University, Denmarkpbour@cs.au.dkNikos M ...

  2. windows 安装 jenkins 自动化构建部署至linux服务器上

    一.环境准备 1.git安装环境 参考链接 https://www.cnblogs.com/yuarvin/p/12500038.html 2.maven安装环境,包括jdk环境安装 参考链接 htt ...

  3. 事务特性,事务的隔离级别以及spring中定义的事务传播行为

    .katex { display: block; text-align: center; white-space: nowrap; } .katex-display > .katex > ...

  4. C++ 动态链接库 dll的加载

    //首先生成一个my.dll项目,在cpp中添加如下代码 //导出函数 _declspec(dllexport) int test(int a, int b) { return a + b; } // ...

  5. docker的安装,自己写了一个安装docker的脚本,辅助做docker安装的实验(ubuntu)

    #!/bin/bash #获取用户名 [ pwd == '/root' ] && hn="root@$(hostname):~#" || hn="root ...

  6. Spring解决循环依赖,你真的懂了吗?

    导读 前几天发表的文章SpringBoot多数据源动态切换和SpringBoot整合多数据源的巨坑中,提到了一个坑就是动态数据源添加@Primary接口就会造成循环依赖异常,如下图: 这个就是典型的构 ...

  7. 组件/ 外层数据初始化时候,不应该触发 on-change 事件

    组件/ 外层数据初始化时候,不应该触发 on-change 事件 watch: { value (value) { this.noOnChange = true // 外层传值 不触发on-chang ...

  8. oracle中plsql练习-----在控制台输出1到100以内的素数。

    一.思路:首先需要知道素数的概念即质数定义为在大于1的自然数中,除了1和它本身以外不再有其他因数. 中心思想是,外循环所有的自然数,内循环折半查询,增加代码的速度,注意:从1开始,需要大于1,但是pl ...

  9. 面向对象第三单元博客(JML)

    // demo/Graph.java package demo; ​ import java.util.ArrayList; ​ public class Demo { /*@ public norm ...

  10. spring给容器中注入组件的几种方式

    目录 环境搭建 spring给容器中注入组件 1.包扫描+组件标注注解(@Controller/@Service/@Repository/@Component)适用于把自己写的类加入组件(默认ID类名 ...