A binary tree is one where each node has, at most, 2 children, creatively named left and right. We define the level of a tree as the number of parent nodes a tree node has. The root of the tree, therefore, is at level 0. Root's children are at level 1, etc. In general, each level of a binary tree can have, at most, 2N nodes, where N is the level of the tree. To be counted as a level, the level must have at least one non-null node. The maximum level of the tree is the last such level.
As an example, consider the tree:
A
/ \
1 2
/ \ \
3 4 C
\
D
At level 2, the tree has the elements 3, 4, and C. The maximum level
of the tree is 3.
For this problem, you'll be given a tree and asked to print out all members of the tree at a given level.
There is an arbitrary amount of space between any two characters of the tree. No single line of input will be more than 80 characters. However, the tree can span multiple lines. Obviously, a tree ends when the () have been balanced. Each test case will always start on a new line.
Oh, one more thing. In the spirit of previous ACM contests, the tree may be arbitrarily long.
For the given level, print out the elements of the tree found on that level. They should appear in the exact same order as they would if you had manually drawn the tree and read off the elements from left to right.
Print the element followed by exactly one space. If a node is empty on that level, then print exactly two spaces (one space for where the node would have been and one space following the node). Empty nodes should be printed only if they have a parent node on the previous level. In other words, don't fill in any values for the "children" of a leaf node. When all the nodes on a given level have been printed, print a newline. Note that this will mean there is one space at the end of line.
If the given level is larger than the maximum level of the tree, then print "No such level" (without the quotes).
3-4---C-
----------D-
No-such-level
--C-