当前位置:网站首页>1064 Complete Binary Search Tree
1064 Complete Binary Search Tree
2022-07-30 21:37:00 【Brosto_Cloud】
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.
p>Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (≤1000). Then N distinct non-negative integer keys are given in the next line. All the numbers in a line areseparated 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 theline.
Sample Input:
101 2 3 4 5 6 7 8 9 0
Sample Output:
6 3 8 1 5 7 9 0 2 4
For BST, the in-order traversal is a non-decreasing sequence, so first sort from small to large, build the tree according to the in-order traversal, and then output according to the serial number (ie, level order traversal):
#include #include using namespace std;int a[1010], n, ans[1010], cnt;void dfs(int x) {if (x <= n) {dfs(x * 2);ans[x] = a[cnt++];dfs(x * 2 + 1);}}int main() {cin >> n;for (int i = 0; i < n; i++) {cin >> a[i];}sort(a, a + n);dfs(1);for (int i = 1; i <= n; i++) {cout << ans[i];if (i != n) {cout << ' ';}}return 0;}
边栏推荐
- 系统结构考点之多级混洗交换网络
- navicat无法连接mysql超详细处理方法
- LeetCode·Daily Question·952. Calculate Maximum Component Size by Common Factor·Union Check
- 【零代码工具】15 款企业级零代码开发平台推荐,总有一款是你心仪的
- 走进Redis,让你重新认识redis。绝不是表面
- IDEA2021.2安装与配置(持续更新)
- MYSQL JDBC Book Management System
- MySQL user authorization
- mysql deadlock
- 【问题】Mysql Waiting for table metadata lock 解决方案 修改lock_wait_timeout时间
猜你喜欢
Image Restoration by Estimating Frequency Distribution of Local Patches
cookie和session区别
kubernetes
活动推荐 | 2022年深圳最值得参加的边缘计算活动
HCIP第十六天
cnpm安装步骤
MySQL 8.0.29 decompressed version installation tutorial (valid for personal testing)
大家都在用的plm项目管理软件有哪些
Outsourcing worked for three years, it was abolished...
ClickHouse 创建数据库建表视图字典 SQL
随机推荐
[Limited Time Bonus] 21-Day Learning Challenge - MySQL from entry to mastery
系统结构考点之并行计算霍纳法则
CISP-PTE真题演示
MySQL user authorization
系统结构考点之PM2I单级网络
Navicat连接MySQL时弹出:1045:Access denied for user ‘root’@’localhost’
登堂入室之soc开发makefile
ValueError: Append mode is not supported with xlsxwriter解决方案
It is enough for MySQL to have this article (disgusting typing 37k words, just for Bojun!!!)
MySQL删除表数据 MySQL清空表命令 3种方法
tcp协议传输中的粘包问题
3 minutes to take you to understand WeChat applet development
MySQL60 homework
The reason for not using bs4 is that the name is too long?Crawl lottery lottery information
uni-app开发微信小程序踩坑
MySql 5.7.38下载安装教程 ,并实现在Navicat操作MySql
Markdown的使用
基于ABP实现DDD--领域逻辑和应用逻辑
TransGAN code reproduction - Jiutian Bisheng Platform
navicat连接MySQL报错:1045 - Access denied for user ‘root‘@‘localhost‘ (using password YES)