Skip to content
MediumTreesAI interview only

Binary Tree Right Side View

Asked atmetaamazonbloombergmicrosoftgoogleadobe

01 · Problem

Given the root of a binary tree, imagine yourself standing on the right side of it. Return the values of the nodes you can see ordered from top to bottom.

02 · Examples

Example 01
    1
   / \
  2   3
   \   \
    5   4
Input
root = [1,2,3,null,5,null,4]
Output
[1,3,4]

At level 0, the rightmost node is 1. At level 1, the rightmost node is 3. At level 2, the rightmost node is 4.

Example 02
  1
   \
    3
Input
root = [1,null,3]
Output
[1,3]

At level 0, the rightmost node is 1. At level 1, the rightmost node is 3.

Example 03
Input
root = []
Output
[]

An empty tree has no visible nodes from any side.

03 · Constraints

  • 01The number of nodes in the tree is in the range [0, 100].
  • 02-100 <= Node.val <= 100

04 · Optimal complexity

Time
O(n)
Space
O(n)
05 · Two ways to work on it

Practice it alone or rehearse it as an interview.

Practice Mode gives you an editor and test runs, nothing else. AI Interview Mode puts a voice interviewer on the other side, adds a clock, and ends with a scored summary of the round.