393. Right Side View
MediumBFS
Print the values visible when looking at the tree from the right: the last node of each level, top to bottom.
Input format
Line 1: n (number of level-order entries) Line 2: the values in level order, -1 for a missing child
Output format
The visible values.
Example 1
Input
7 1 2 3 -1 5 -1 4
Output
1 3 4
Example 2
Input
3 1 -1 3
Output
1 3
Constraints
1 ≤ nodes ≤ 10⁴
Read input from standard input and print the answer to standard output. Your code is judged against 5 test cases, most of them hidden.
Loading the MyLoginn IDE…