Medium · Trees

Binary Tree Right Side View

Given the root of a binary tree, return the values of the nodes visible from its right side — the rightmost node at each depth — ordered from top to bottom (an empty tree gives []).

Examples

Example 1

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

Output: [1, 3, 4]

Example 2

[1,·,3] right-leaning

Output: [1, 3]

Rebuild it in the studio

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More Trees problems