Binary Tree Right Side View

Free with account Updated Nov 19, 2026 · 5 min read
On this page

    Problem statement

    Given the root of a binary tree, imagine standing to its right and looking left. Return the values you can see, from top to bottom. On each level you see exactly one node: the one furthest to the right on that level.

    A short, popular tree question. It checks whether you think in levels rather than in "go right". Original problem: LeetCode 199.

    Example

    Looking from the right you see 1, 3, 5 and 6. Nodes 5 and 6 are in the left subtree, but nothing on their level is further right.
    Looking from the right you see 1, 3, 5 and 6. Nodes 5 and 6 are in the left subtree, but nothing on their level is further right.
    Input:  root = [1, 2, 3, 4, 5, null, null, null, null, 6]
    Output: [1, 3, 5, 6]
    Input:  root = [1, null, 3]
    Output: [1, 3]
    Input:  root = []
    Output: []

    Constraints (assumed)

    • 0 ≤ number of nodes ≤ 100.
    • −100 ≤ node value ≤ 100.

    What this question tests

    • Level-order traversal (BFS) with clean level boundaries.
    • Noticing that the visible node on a level can come from the left subtree.
    • Knowing a DFS alternative and comparing the space each one uses.

    Questions to ask first

    1. What should an empty tree return? (An empty list.)
    2. Top to bottom order? (Yes, one value per level, root first.)
    3. Is it really one node per level, the rightmost? Some versions ask for both the left and right views; confirm which.
    4. How big can the tree be? For a very deep tree, recursion depth matters for the DFS version.