Skip to content
AI360Xpert
Beta
Difficulty: MediumTrees

Binary Tree Right Side View

Problem in Plain English

Given the root of a binary tree, return the values visible when looking from the right side, which is exactly the rightmost node of each level from top to bottom.

Problem Statement

Given the root of a binary tree, imagine standing on its right side and return the values you can see from top to bottom.

Exactly one node per level is visible: the rightmost node at that depth, even when it sits in a left subtree.

Constraints

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

Examples

Example 1

Input
root = [1,2,3,null,5,null,4]
Output
[1,3,4]

Depth 0 contains 1; depth 1 contains 2 then 3; depth 2 contains 5 then 4. Taking the rightmost value from each depth gives [1,3,4].

Example 2

Input
root = [1,null,3]
Output
[1,3]

Each depth has only one node, so both 1 and 3 are visible.

Example 3

Input
root = []
Output
[]

There are no levels in an empty tree, so the view is an empty list.

Example 4

Input
root = [1,2,3,4,null,null,null,5]
Output
[1,3,4,5]

Node 3 hides 2 at depth 1, but its subtree stops there. At depths 2 and 3, nodes 4 and 5 in the left subtree are the only existing nodes and remain visible.

Example 5

Input
root = [7,7,7,null,7]
Output
[7,7,7]

Visibility depends on position, not distinct values. Each depth contributes a 7, so repeated values must stay in the answer.

Intuition

The right side view chooses one node at each depth: the rightmost existing node, regardless of its value or whether its path follows right-child links. In Example 1, depth 2 contains 5 and 4, so 4 hides 5. Breadth-first search can take the last node of each left-to-right level; depth-first search can visit right children first and take the first node at each new depth. Both must still explore left subtrees, because those may extend below every node on the right.

Approaches

Level Order Last Node

Solution Details

Reveal Level Order Last Node: intuition, complexity, and code

Hints

Hint 1
One node per level survives: the rightmost one at that depth.
Hint 2
In breadth-first order, the visible node is simply the last one dequeued in each level batch.
Hint 3
In depth-first order, always go right first and record the first node seen at each new depth.

Edge Cases

  • An empty tree returns []; a single node contributes its value once.
  • A left-only chain still shows every node: each level has only one candidate.
  • A left subtree extending below the right subtree contributes visible nodes at those deeper levels, as in [1,2,3,4,null,null,null,5].
  • Two nodes at the same depth with equal values still represent different positions; choose the rightmost node and do not deduplicate answer values.
  • Negative and zero values affect neither ordering nor visibility.
  • A 100-node chain gives 100 output values and maximum recursion depth; a broad tree instead maximizes breadth-first queue storage.

Common Mistakes and Interview Tips

  • Following only right-child links, missing deeper visible nodes in the left subtree.
  • Recording every node of a level or the first node in a left-first breadth-first queue instead of its last node.
  • Testing the changing queue size inside the loop, mixing children into their parent level.
  • Traversing left first while recording first arrivals, which produces the left side view.
  • Stopping DFS at an already-recorded depth instead of merely skipping its value, which loses deeper visible descendants.
  • Choosing the largest value at a level instead of the rightmost position, or deduplicating repeated values.
  • Using JavaScript shift for every removal, or keeping consumed array references forever while claiming width-bounded queue storage.
  • Keeping result as persistent solution state without clearing it between calls.

Key Takeaway

A per-level view is a traversal plus a selection rule: left-to-right BFS takes the last node, while right-first DFS takes the first node at each depth. Visit all subtrees even when the current depth already has a selected value, because deeper levels may still need an answer.