当前位置:网站首页>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;}
边栏推荐
- 手把手教你搭建一台永久运行的个人服务器
- 解决npm warn config global `--global`, `--local` are deprecated. use `--location=global` instead
- openim支持十万超级大群
- 关于MySQL主从复制的数据同步延迟问题
- 活动推荐 | 2022年深圳最值得参加的边缘计算活动
- The reason for not using bs4 is that the name is too long?Crawl lottery lottery information
- mysql去除重复数据
- About the data synchronization delay of MySQL master-slave replication
- [Deep Learning] Understanding of Domain Adaptation in Transfer Learning and Introduction of 3 Techniques
- 3 minutes to take you to understand WeChat applet development
猜你喜欢
Advanced c language: pointers (5)
cmd (command line) to operate or connect to the mysql database, and to create databases and tables
A simple rich text editor
kubernetes
这本记述40年前历史的游戏书,预言的却是当下的事
【信息安全技术】RSA算法的研究及不同优化策略的比较
基于ABP实现DDD--领域逻辑和应用逻辑
Quick Master QML Chapter 6 Animation
About the data synchronization delay of MySQL master-slave replication
MySql创建数据表
随机推荐
mysql remove duplicate data
JUC原子类详解
3分钟带你了解微信小程序开发
8 ways to get element attributes in JS
Google Earth Engine ——ee.List.sequence函数的使用
TransGAN代码复现—九天毕昇平台
LeetCode · 23. Merge K ascending linked lists · recursion · iteration
kubernetes
CISP-PTE真题演示
navicat新建数据库
Union, the difference between union and structure, the knowledge of enumeration of C language corners
大家都在用的plm项目管理软件有哪些
(7/29)基础板子最小生成树prim+kruskal
LeetCode·每日一题·952.按公因数计算最大组件大小·并查集
Generate OOM records in a production environment. Conclusion: Don't be lazy to query useless fields unless you are completely sure.
How strict Typescript strict mode?
一个网络两种用途!南开&哈工程提出TINet,通过细化纹理和边缘,在显著性目标检测和伪装目标检测上实现双SOTA!...
Google Earth Engine ——我们如何筛选一个列表中的排序以时间为例
Chrome 配置samesite=none方式
Quick Master QML Chapter 6 Animation