当前位置:网站首页>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 0Sample Output:
6 3 8 1 5 7 9 0 2 4For 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;} 边栏推荐
- MySQL分页查询的5种方法
- MySQL 8.0.29 设置和修改默认密码
- Markdown的使用
- 【Network Security Column Directory】--Penguin Column Navigation
- MySQL删除表数据 MySQL清空表命令 3种方法
- 解决centos8 MySQL密码问题ERROR 1820 (HY000) You must reset your password using ALTER USER
- Apache DolphinScheduler新一代分布式工作流任务调度平台实战-
- IDEA2018.3.5 cancel double-click Shift shortcut
- cnpm安装步骤
- How strict Typescript strict mode?
猜你喜欢

Deep Non-Local Kalman Network for VideoCompression Artifact Reduction

MySQL compressed package installation, fool teaching
![[Deep Learning] Understanding of Domain Adaptation in Transfer Learning and Introduction of 3 Techniques](/img/51/b351385c1f0f4e0a545e54c8ae7491.png)
[Deep Learning] Understanding of Domain Adaptation in Transfer Learning and Introduction of 3 Techniques

MySQL cursors

TransGAN代码复现—九天毕昇平台

cmd(命令行)操作或连接mysql数据库,以及创建数据库与表

JUC原子类详解

How strict Typescript strict mode?

Deep Kalman Filter Network for Video Compression Artifact Removal

Automatically generate test modules using JUnit4 and JUnitGenerator V2.0 in IDEA
随机推荐
MySQL60 homework
设备树的引入与体验
系统结构考点之并行主存
Teach you how to build a permanently running personal server
How do I refresh the company's background management system (Part 1) - performance optimization
MySQL user authorization
c语言进阶篇:指针(五)
openim支持十万超级大群
(7/29) Basic board minimum spanning tree prim+kruskal
kubernetes
【Network Security Column Directory】--Penguin Column Navigation
How strict Typescript strict mode?
基于ABP实现DDD--实体创建和更新
MySQL分页查询的5种方法
深入浅出富文本编辑器
socket: Kernel initialization and detailed process of creating streams (files)
Apache DolphinScheduler新一代分布式工作流任务调度平台实战-
Outsourcing worked for three years, it was abolished...
A simple rich text editor
冲刺第六周