Skip to content
Hello Python

Checking your account…

Sign in to save your code and progress across devices. The lesson and problem statement remain public.

Loading the interactive Interview workspace.If it does not appear, the problem and learning material remain readable, but browser execution is unavailable.Reload Interview workspace

Problem

Implement Solution.rightSideView(root). Return the node value visible from the right side at every depth from top to bottom.

Starter code

class Solution:
    def rightSideView(self, root):
        pass
Test cases

sample

{
  "args": [
    {
      "$type": "binary-tree",
      "values": [
        1,
        2,
        3,
        null,
        5,
        null,
        4
      ]
    }
  ]
}

Expected: [1,3,4]

Wizard outline
  1. Step 1: Initialize Solution.rightSideView

    Replace the empty starter with the first real state owned by Solution.rightSideView. A small, named state is easier to verify than a complete algorithm. Establish it before adding the branch or loop that changes it.

  2. Step 2: Assemble the primary transition

    Extend the initialized state with the next contiguous part of the popular solution. The transition explains how one input element or operation changes the state; boundaries are easier to reason about after this invariant is visible.

  3. Step 3: Pass the Sample case

    Complete the readable core algorithm for one representative Interview case. Process fixed BFS levels and record the last node in natural left-to-right order.

  4. Step 4: Harden the Left Only Depth boundary

    Repair the reviewed boundary and pass the complete submission contract. BFS groups equal depths and natural enqueue order is left-to-right, so the final node of each level is exactly the rightmost visible node at that depth.

Footguns and prerequisites
  • The visible node can be in a left subtree when no right-side node exists at that depth.
  • trees and graphs
Reviewed references
Practice prerequisites
  • Expand One Tree Level(opens in a new tab)

    Expand One Tree Level isolates at the start of each outer iteration, the queue contains exactly the current level in left-to-right order. That focused state discipline is required when implementing binary tree right side view as a complete Interview Problem.

Recommended approach and implementation

BFS left-to-right by levels and append the value processed at index level_size-1.

Why it works: BFS groups equal depths and natural enqueue order is left-to-right, so the final node of each level is exactly the rightmost visible node at that depth.

class Solution:
    def rightSideView(self, root):
        """
        Checkpoint 1: initialize the state owned by this Interview contract.
        Checkpoint 2: assemble the primary transition without hiding the boundary.
        """
        if root is None: return []
        from collections import deque
        queue = deque([root])
        view = []
        while queue:
            level_size = len(queue)
            for index in range(level_size):
                node = queue.popleft()
                if node.left: queue.append(node.left)
                if node.right: queue.append(node.right)
                if index == level_size - 1: view.append(node.val)
        return view
Similar exercises