A gantry crane frame is modeled as a binary tree; each level of the tree is one horizontal row of the frame, and each node carries a (possibly negative) load reading. For every row from top to bottom, report the largest load reading found anywhere on that row.
Line 1: an integer n, the number of tokens on the next line.
Line 2: n space-separated tokens describing a binary tree in level-order (breadth-first). The first token is the root's value. Reading left to right, keep a queue of already-created nodes; for each node taken from the front of the queue, the next two tokens are its left child then its right child, where the token null marks a missing child. Only non-null children are added to the queue. Trailing null tokens for absent children at the deepest level may be omitted. Every node value is an integer.
A single line: the maximum node value on each level from top to bottom, space-separated.
Example 1
Input
7 3 9 20 null null 15 7
Expected
3 20 15
Explanation
Row maxima are max(3)=3, max(9,20)=20, and max(15,7)=15. Output: 3 20 15.
Example 2
Input
3 -5 -2 -8
Expected
-5 -2
Explanation
Row 0 max is -5; row 1 holds -2 and -8, whose max is -2. Output: -5 -2.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →