LogiQuest

379. Zigzag Level Order

MediumBFS

Print the levels left to right, then right to left, alternating, one line per level.

Input format

Line 1: n (number of level-order entries) Line 2: the values in level order, -1 for a missing child

Output format

One line per level.

Example 1

Input

7
3 9 20 -1 -1 15 7

Output

3
20 9
15 7

Example 2

Input

7
1 2 3 4 5 6 7

Output

1
3 2
4 5 6 7

Constraints

1 ≤ nodes ≤ 10⁴

Read input from standard input and print the answer to standard output. Your code is judged against 3 test cases, most of them hidden.

Loading the MyLoginn IDE…