Problem B
Constructing BST
Input: Standard Input
Output: Standard Output
Normally, we construct BST by successively inserting an element. In that case, the ordering of elements has great impact on the structure of the tree. Look at the following cases:
In this problem, you have to find the order of 1 to N integers such that the BST constructed by them has height of at most H. The height of a BST is defined by the following relation –
1. BST having no node has height 0.
2. Otherwise, it is equal to the maximum of the height of the left sub-tree and right sub-tree plus 1.
Again, several orderings can satisfy the criterion. In that case we prefer the sequence where smaller numbers come first. For example, for N=4, H=3 we want the sequence 1 3 2 4 rather than 2 1 4 3 or 3 2 1 4.
Output of each test case
should consist of a line starting with “Case #: “where ‘#’ is the test case
number. It should be followed by the sequence of N integers in the same line. There must not be any
trailing space at the end of the line. If it is not possible to construct such
tree then print “Impossible.” (without the quotes).
|
4 3 4 1 6 3 0 0 |
Case
1: 1 3 2 4 Case
2: Impossible. Case
3: 3 1 2 5 4 6 |
Problem setter: Md. Kamruzzaman
Special Thanks: Syed Monowar Hossain