Day 59 of 90MediumLeetCode

Binary Tree Right Side View

List the values you'd see standing to the right of a binary tree, one per level, from top to bottom.

Pattern
BFS, last node per level
Time box
25 min
Time
O(n)
Space
O(n)

Try it first — give it 25 minutes

0/4 revealed

Stuck? Take one hint at a time. The struggle is where the pattern sticks.

    The problem in plain English

    You get the root of a binary tree. Imagine standing to the right of it and looking left. On each level you can see only the rightmost node; it hides the others. Return the values you see, from the top level down.

    An empty tree gives []. The tree has up to 100 nodes.

    Example: [1, 2, 3, null, 5, null, 4] → [1, 3, 4].

          1        ← see 1
         / \
        2   3      ← see 3
         \   \
          5   4    ← see 4 (5 is hidden behind it)

    Start with brute force

    The usual wrong first idea isn't slow; it's incorrect: "start at the root and keep going right". On the tree above, that gives 1 → 3 → 4, which happens to be right. Now try [1, 2, 3, 4]:

          1        ← see 1
         / \
        2   3      ← see 3
       /
      4            ← see 4

    Going right gives [1, 3] and stops. But from the right side you can still see 4, because nothing on the right reaches that deep. The visible node of a level can be in the left subtree. So you need to look at every level properly.

    A correct but roundabout approach: compute the full level-order list (Binary Tree Level Order Traversal), then take the last item of each level. It's O(n) and fine, but it stores every value only to throw most of them away.

    Building the intuition

    The definition says "one node per level". So think in levels, and that means BFS.

    In level-order BFS, nodes leave the queue left to right within a level, because each level's children are queued left child first. When you freeze the queue size at the start of a round, you know exactly how many nodes belong to this level. So you also know which pop is the last one: pop number size − 1. That's the rightmost node, the one you'd see.

    So it's Binary Tree Level Order Traversal with one change: instead of collecting the whole level, record only its last node.

    The optimal approach

    1. If root is null, return [].
    2. Put root in a queue.
    3. While the queue isn't empty:
      1. size = len(queue).
      2. Pop size nodes with index i from 0 to size − 1. Push each one's non-null children, left before right.
      3. When i == size − 1, append that node's value to the result.
    4. Return the result.

    The subtle detail: push the left child before the right. The "last pop is the rightmost" claim depends on that order. If you push right first, the last pop is the leftmost node, and you've built the left side view.

    The code

    Every solution below is tested against the examples and edge cases before it ships. Pick your language — we'll remember it.

    from collections import deque
    
    
    class Solution:
        def rightSideView(self, root: Optional[TreeNode]) -> List[int]:
            result = []
            if root is None:
                return result
    
            queue = deque([root])
            while queue:
                size = len(queue)  # exactly one level is in the queue right now
                for i in range(size):
                    node = queue.popleft()
                    if i == size - 1:
                        # Left children are queued before right ones, so the last
                        # node of a level is the rightmost one: the one you can see.
                        result.append(node.val)
                    if node.left:
                        queue.append(node.left)
                    if node.right:
                        queue.append(node.right)
    
            return result

    Dry run

    Tree [1, 2, 3, 4] (the one that breaks "keep going right").

    RoundQueue at startsizePops (pushes)Last pop → recorded
    1[1]11 (push 2, 3)1
    2[2, 3]22 (push 4), 3 (none)3
    3[4]14 (none)4

    Result: [1, 3, 4]. The 4 came from the left subtree, and BFS found it without any special case.

    Complexity

    • Time: O(n). Every node is pushed and popped once.
    • Space: O(n). The queue holds up to one full level, which can be about n/2 nodes in a complete tree. The answer holds one value per level.

    Edge cases and traps

    • Only following right children. It misses visible nodes in the left subtree, as in [1, 2, 3, 4].
    • Empty tree. Return [] before touching the queue.
    • Wrong push order. Pushing right before left flips which node is last.
    • Reading the size inside the loop. Freeze it at the start of each round, or levels blur together.

    Say it in the interview

    "The view shows the rightmost node of each level, and that node can be in the left subtree if the right side is shorter. So I do a level-order BFS. At the start of each round, I freeze the queue size, which is the number of nodes on this level, and since children go in left before right, the last node I pop in the round is the rightmost. I record its value. That's O(n) time and O(n) space for the queue."

    Follow-ups interviewers ask

    • "Can you do it with DFS?" Yes. Recurse with dfs(node, depth), visiting the right child before the left. The first node you reach at each new depth is the rightmost one: if depth == len(result), append it. That's O(n) time and O(h) stack space, which is better than BFS for wide trees.
    • "Left side view?" Record the first pop of each round instead of the last (or do the DFS going left first).

    Practise the same idea

    This is Day 59 of a free 90-day plan 🐸

    90 problems in the order that builds on itself, one a day, with a walkthrough like this for each. Tick them off and keep your streak.

    Get the full roadmap — free