Loading...
Loading...
On sports day, the houses march past in rows arranged as a tree. The announcer reads the names row by row like a snake: the first row from left to right, the second from right to left, the third from left to right again, and so on.
Print the jersey numbers in the order they are read, one row per line.
N for no node. Children are listed only for real nodes, and N tokens at the end of the line may be left out. For example, 1 2 3 N 4 is root 1 with children 2 and 3, and 4 is the right child of 2. The tree has at least one node.Example 1
Input
3 9 20 N N 15 7
Output
3 20 9 15 7
Level 1: 3. Level 2 is read right to left: 20 9. Level 3 is read left to right: 15 7.
Example 2
Input
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Output
1 3 2 4 5 6 7 15 14 13 12 11 10 9 8
Level 3 goes back to left to right: 4 5 6 7. Level 4 is reversed.
Try the problem first. Open one hint at a time.
First get each level in plain left-to-right order, as in a normal level-by-level walk.
Then print every second level backwards. Keep a flag that flips after each level.
Changing the order in which you add children to the queue does not work: it breaks the order of the next level as well.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor