All problems

Problem 402

Largest Value in Each Row

Medium · 20 pts · Tree, Breadth-First Search, Binary Tree

Problem

For every row (level) of a binary tree, from the top down, find the largest value in that row. The first row contains only the root, the second row the root's children, and so on. Print these maxima in order. Values may be negative, so start each row's maximum from that row's first value rather than from 0.

Input

The first line contains N, the number of tokens. The second line contains N tokens describing the tree in level order (breadth-first, left to right), where 'null' marks a missing child, as on LeetCode.

Output

Print the maximum of each row, from top to bottom, separated by spaces.

Constraints

1 <= number of nodes <= 10000; -1000000000 <= value <= 1000000000

Example input

7
1 3 2 5 3 null 9

Example output

1 3 9

Explanation

In the tree 1 / 3 2 / 5 3 _ 9, the rows are [1], [3 2] and [5 3 9], whose largest values are 1, 3 and 9.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Level Sums of a Binary Tree Next: Count Leaf Nodes