(浙江大学数据结构)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
文章目录 题目分析 题目链接 题目分析 题意重述:给定一棵二叉树的结构,和待填的数值,请将数据填到二叉树中的结点中,使之满足二叉搜索树的性质. 然后按照层序遍历输出数值. 分析: 本题分两步. 第一步 ...
最新文章
- JAVA_HOME PATH CLASSPATH
- 罚函数法求解约束问题最优解
- 织梦根目录下面404页面,主页能正常运行404页面,切换至栏目页404页面内的图片不能正常显示,解决...
- Kafka使用入门教程
- 2021-10-16 集合(set)与映射(map) 恋上数据结构笔记
- ASP.NET MVC 框架路线图更新 【转】
- 反思代码能力提升方法:重构 多写 知识面
- php使用Header函数,PHP_AUTH_PW和PHP_AUTH_USER做用户验证及缺点
- android代码生成excel,AndroidExcel
- 见过的最详强的讲ESP定律脱壳(推荐)
- 第41天:匀速、缓动运动和图片无缝滚动
- 视觉SLAM十四讲_3-李群和李代数
- Atitit 品牌之道 attilax著 艾龙 著 1. 第1章 品牌和品牌管理	1 2. 第Ⅱ篇 制定品牌战略	2 3. 第Ⅲ篇 品牌营销活动:设计与执行	2 4. 第Ⅳ篇 评估和诠释品牌绩效	3
- Excel数据批量导入导出(基础版)
- Windows下Mingw版本的FFMPEG源码编译
- 云原生--k8s之pod
- 神武授权位置服务器,太古封魔录神武获得,神武服务器入口
- 树莓派:GPIO/引脚/Pin 介绍
- 写论文第一步——查文献的方法
- SQL注入攻击以及防护