#P8603. [蓝桥杯 2013 国 C] 横向打印二叉树

    ID: 7599 远端评测题 1000ms 64MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>模拟树形数据结构2013蓝桥杯国赛

[蓝桥杯 2013 国 C] 横向打印二叉树

Description

The idea is simple: when inserting a new node into a binary search tree, first compare it with the root. If it is smaller, pass it to the left subtree to continue processing; otherwise, pass it to the right subtree.

When an empty subtree is reached, place the node at that position.

For example, with the input order 10 8 5 7 12 4, the binary tree should be built as shown in Figure 11.

This problem requires: given the numbers, build a binary search tree, and print the tree horizontally to standard output.

Input Format

The input is one line of NN integers separated by spaces. N<100N < 100, and each number does not exceed 1000010000.

NN is not given in the input.

There are no duplicate numbers in the input.

Output Format

Output the horizontal representation of the binary search tree. To make it easier for the judging program to compare the number of spaces, please replace spaces with periods.

10 8 5 7 12 4
...|-12
10-|
...|-8-|
.......|...|-7
.......|-5-|
...........|-4
10 5 20
...|-20
10-|
...|-5
5 10 20 8 4 7
.......|-20
..|-10-|
..|....|-8-|
..|........|-7
5-|
..|-4

Hint

Sample Explanation 1:

Time limit: 1 second, 64 MB. Lanqiao Cup 2013, the 4th National Finals.

Translated by ChatGPT 5