#P8603. [蓝桥杯 2013 国 C] 横向打印二叉树
[蓝桥杯 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 .
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 integers separated by spaces. , and each number does not exceed .
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
京公网安备 11011102002149号