题目:
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

本题目题意是输入n个数,将其放入完整的二叉查找树中。
思路为先进行排序,然后应用中序遍历,(利用完整二叉树的左子为2n,右子树的右子为2n+1, )依次输入各个数即可。

代码为:

#include<stdio.h>#define MAXN 1000 int n;
int index = 0;void inOrder(int root,int a[],int finalresult[]);
int main()
{int a[MAXN];int finalresult[MAXN+1];//*按照习惯完整二叉树是从1开始的 故此处用MAXN+1*//int i,j,t;scanf("%d",&n);for(i=0;i<n;i++){scanf("%d",&a[i]);}//*冒泡法排序*//for(j=1;j<n;j++){for(i=0;i<n-j;i++){if(a[i] > a[i+1]){t = a[i];a[i]=a[i+1];a[i+1] = t;    }}}inOrder(1,a,finalresult);for(i=1;i<=n;i++){printf("%d",finalresult[i]);if(i<n) printf(" ");}return 0;  }//*将a按照完整二叉树的序号放入finalresult*//
void inOrder(int root,int a[],int finalresult[])
{if(root>n) return;inOrder(root*2,a,finalresult);finalresult[root] = a[index++];inOrder(root*2+1,a,finalresult);
}

(浙江大学数据结构)PTA Complete Binary Search Tree (10 分)相关推荐

  1. [浙大数据结构] 04-树6 Complete Binary Search Tree (30分)

    1 题目描述 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following pr ...

  2. C++学习之路 | PTA(甲级)—— 1064 Complete Binary Search Tree (30分)(带注释)(精简)

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

  3. PAT甲级1064 Complete Binary Search Tree (30分):[C++题解]完全二叉搜索树BST

    文章目录 题目分析 题目链接 题目分析 思路: 第一步,构造含有n个结点的完全二叉树:第二步,将n个数值填入,使其满足二叉搜索树的性质. 对于第一步: 完全二叉树用一维数组可以存下,不过从根结点的下标 ...

  4. 1064 Complete Binary Search Tree (30 分)【难度: 一般 / 知识点: 完全二叉搜索树】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805407749357568 二叉搜索数的中序遍历是有序的.故先将权值排序 ...

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

    title: 04-树6 Complete Binary Search Tree(30 分) date: 2017-11-12 14:20:46 tags: - 完全二叉树 - 二叉搜索树 categ ...

  6. C++学习之路 | PTA(甲级)—— 1043 Is It a Binary Search Tree (25分)(带注释)(精简)

    1043 Is It a Binary Search Tree (25分) A Binary Search Tree (BST) is recursively defined as a binary ...

  7. C++学习之路 | PTA(甲级)—— 1099 Build A Binary Search Tree (30分)(带注释)(精简)

    1099 Build A Binary Search Tree (30分) A Binary Search Tree (BST) is recursively defined as a binary ...

  8. PAT (Advanced Level) Practice 1043 Is It a Binary Search Tree (25 分) 凌宸1642

    PAT (Advanced Level) Practice 1043 Is It a Binary Search Tree (25 分) 凌宸1642 题目描述: A Binary Search Tr ...

  9. PAT甲级1099 Build A Binary Search Tree (30分):[C++题解]建立二叉搜索树、dfs和bfs

    文章目录 题目分析 题目链接 题目分析 题意重述:给定一棵二叉树的结构,和待填的数值,请将数据填到二叉树中的结点中,使之满足二叉搜索树的性质. 然后按照层序遍历输出数值. 分析: 本题分两步. 第一步 ...

最新文章

  1. 都在说微服务,那么微服务的反模式和陷阱是什么(三)
  2. python对文件的读操作方法有哪些-Python中文件的读取和写入操作
  3. shell编程中的select用法
  4. 存储器的保护(三)——《x86汇编语言:从实模式到保护模式》读书笔记20
  5. Javascript 随机数函数 学习之一:产生服从均匀分布随机数
  6. [转]JDK动态代理
  7. CSS布局讲解-float浮动布局使用
  8. Camera系统之ISP综述(一)
  9. Oracle使用systimestamp取微秒
  10. java poi操作word转pdf
  11. Java代码实现幸运抽奖
  12. C语言 数组排序 打乱 查找
  13. 中国月球探测标识确定 寓龙的传人登月梦
  14. 将iPhone投影到Mac上
  15. python小白不要怕,小编来保护你~
  16. 凸包问题 —— Graham扫描法
  17. w7系统怎么开启打印机服务器,Win7如何开启打印机服务?
  18. QCC305X-QCC304x系列开发教程(系统篇)之3.4-Mirroring详解
  19. oss视频转码处理(解决部分浏览器无法正常播放问题)
  20. vscode找不到执行文件导致更新失败

热门文章

  1. 十分钟使用苹果机器学习框架CoreML进行图片识别(Swift版)
  2. Selenium自动翻页爬取证券公司公告信息——以中国平安为例
  3. android 广播单词锁屏,[android]利用碎片化时间记单词———单词锁屏
  4. 用户登录动态切换头像及切换名字
  5. 猜歌?没有我猜不到的歌!因为我有Python脚本!
  6. QP/C API 参考
  7. 数据结构(C语言版)严蔚敏->排序
  8. UVCCamera gradle配置
  9. 【动态规划】宠物小精灵之收服
  10. 刷PDD笔试题--编程