Binary Tree Right Side View

Medium

Topics
TreeBFSDFS

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

Example 1

25134
visible from the right
One node per level — the rightmost — so the answer has 3 entries.
Tree given in level-order (breadth-first); null marks a missing child.
Input:  root = [1,2,3,null,5,null,4]
Output: [1,3,4]

Example 2

13
visible from the right
Input:  root = [1,null,3]
Output: [1,3]

Constraints

  • 0 <= number of nodes <= 100
  • -100 <= Node.val <= 100
Editorial — intuition, approach, complexity and pitfalls

Intuition

From the right you only see the last node of each level. So a level-order BFS, keeping the rightmost node of each level, is exactly the right-side view.

Approach

  1. BFS level by level.
  2. For each level, the last node dequeued is the visible one — record it.
  3. Collect these across all levels.

Dry Run

tree 1 2 3 null 5 null 4
      1
     / \
    2   3
     \    \
      5     4
  level [1]      take the last, 1       res [1]
  level [2, 3]   take the last, 3       res [1, 3]
  level [5, 4]   take the last, 4       res [1, 3, 4]
answer 1 3 4
The right side view is "the last node of each level", not "keep going right from
the root". On this tree the two happen to agree, both giving 1 3 4, so it is worth
having a tree where they do not. On 1 2 3 4 -- a 4 hanging off 2's left -- the
view is 1 3 4, because 4 is the only node on level 3, while walking right from the
root stops at 1 3. The view is whatever is rightmost at each DEPTH, no matter
which parent it hangs from or which side of that parent it is on.
Take the first of each level instead and you get the left side view, 1 2 5. Same
loop, one index changed.
The level size must be snapshotted before the inner loop for the same reason as
plain level-order: read len(q) live and the boundary dissolves, because the
children appended for the next level are counted as part of this one.

Complexity

  • Time — O(n): each node visited once.
  • Space — O(n): the BFS queue.

Common Pitfalls

  • Take the last node of each level. Taking the first gives the left side view -- 1 2 5 where the right side is 1 3 4.
  • Snapshot the level size before the inner loop. Reading len(q) inside it sees the children you just appended, so the level boundary dissolves and levels run together.

Full Solution — all languages

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

class Solution:
    def rightSideView(self, root):
        if not root: return []
        from collections import deque
        res=[]; q=deque([root])
        while q:
            n=len(q)
            for i in range(n):
                node=q.popleft()
                if i==n-1: res.append(node.val)
                if node.left: q.append(node.left)
                if node.right: q.append(node.right)
        return res
SensAI
Beta
Tap the mic to stop voice mode