Loading...
Loading...
A start-up in Bengaluru keeps its org chart as a tree. The CEO is at the top, and each person manages at most two people. HR wants to print the chart for the notice board, one level of the company per line.
Print the employee IDs level by level. The first line has the CEO. Each next line has everyone one level lower, from left to right.
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
1 2 3 N 4 5 6
Output
1 2 3 4 5 6
Level 1 is the CEO 1. Level 2 is 2 and 3. Level 3 is 4, then 5 and 6 from left to right.
Example 2
Input
3 9 20 N N 15 7
Output
3 9 20 15 7
9 has no reports, so level 3 holds only the reports of 20.
Try the problem first. Open one hint at a time.
A queue gives people in the order they were added: first in, first out.
Start the queue with the CEO. When you take someone out, add their left and then right report.
To split the lines, note the queue size at the start of a level. Take out exactly that many people for this line.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor