(浙江大学数据结构)PTA Complete Binary Search Tree (10 分)
题目:
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 分)相关推荐
- [浙大数据结构] 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 ...
- 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 ...
- PAT甲级1064 Complete Binary Search Tree (30分):[C++题解]完全二叉搜索树BST
文章目录 题目分析 题目链接 题目分析 思路: 第一步,构造含有n个结点的完全二叉树:第二步,将n个数值填入,使其满足二叉搜索树的性质. 对于第一步: 完全二叉树用一维数组可以存下,不过从根结点的下标 ...
- 1064 Complete Binary Search Tree (30 分)【难度: 一般 / 知识点: 完全二叉搜索树】
https://pintia.cn/problem-sets/994805342720868352/problems/994805407749357568 二叉搜索数的中序遍历是有序的.故先将权值排序 ...
- 04-树6 Complete Binary Search Tree(30 分)
title: 04-树6 Complete Binary Search Tree(30 分) date: 2017-11-12 14:20:46 tags: - 完全二叉树 - 二叉搜索树 categ ...
- 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 ...
- 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 ...
- 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 ...
- PAT甲级1099 Build A Binary Search Tree (30分):[C++题解]建立二叉搜索树、dfs和bfs
文章目录 题目分析 题目链接 题目分析 题意重述:给定一棵二叉树的结构,和待填的数值,请将数据填到二叉树中的结点中,使之满足二叉搜索树的性质. 然后按照层序遍历输出数值. 分析: 本题分两步. 第一步 ...
最新文章
- 都在说微服务,那么微服务的反模式和陷阱是什么(三)
- python对文件的读操作方法有哪些-Python中文件的读取和写入操作
- shell编程中的select用法
- 存储器的保护(三)——《x86汇编语言:从实模式到保护模式》读书笔记20
- Javascript 随机数函数 学习之一:产生服从均匀分布随机数
- [转]JDK动态代理
- CSS布局讲解-float浮动布局使用
- Camera系统之ISP综述(一)
- Oracle使用systimestamp取微秒
- java poi操作word转pdf
- Java代码实现幸运抽奖
- C语言 数组排序 打乱 查找
- 中国月球探测标识确定 寓龙的传人登月梦
- 将iPhone投影到Mac上
- python小白不要怕,小编来保护你~
- 凸包问题 —— Graham扫描法
- w7系统怎么开启打印机服务器,Win7如何开启打印机服务?
- QCC305X-QCC304x系列开发教程(系统篇)之3.4-Mirroring详解
- oss视频转码处理(解决部分浏览器无法正常播放问题)
- vscode找不到执行文件导致更新失败