AlgoFrames

Right side view

Standard · Write code · about 10 min

You stand to the right of a binary tree and look at it. Return the values of the nodes you can see, from top to bottom. On each level you see only the rightmost node.

Example 1

Input:

[[1,2,3,null,5,null,4]]

Output:

[1,3,4]

On level 2 you see 4, the rightmost node, not 5.

Example 2

Input:

[[1,2,3,4]]

Output:

[1,3,4]

4 hangs on the left branch, but nothing on its level is further right.

Constraints

  • 0 ≤ number of nodes ≤ 1,000
  • -1,000 ≤ Node.val ≤ 1,000
  • The tree is given in level order; null marks a missing child

Learn the idea first: Level-order traversal, a visual lesson.

More Level-order traversal problems