199. Binary Tree Right Side View¶
Edited by Charles
Medium · Python · Binary Tree
Problem
Read the original problem on LeetCode.
The official page contains the problem statement, examples, and constraints.
Explanation, examples & other approaches
Core idea
Given the root of a binary tree, imagine yourself standing on the right side of the tree.
Return the values of the nodes that you can see, ordered from top to bottom.
Original study notes follow, including intermediate snippets and earlier attempts.
Problem¶
Given the root of a binary tree, imagine yourself standing on the right side of the tree.
Return the values of the nodes that you can see, ordered from top to bottom.
Example¶
From the right side, we can see:
Therefore:
The key observation is:
For every level of the binary tree, we only need the rightmost node.
Therefore, this problem can be solved using BFS (Breadth-First Search) / Level Order Traversal.
Normally, level order traversal processes nodes:
But in this solution, we intentionally put the right child into the queue first:
Therefore, the first node processed at each level is the rightmost node.
We add that node to the result.
If the tree is empty:
Otherwise, initialize the queue with the root:
and create the result:
We use:
At the beginning of every iteration, queue contains all nodes from the current level.
For example:
Initially:
After processing the first level:
Notice that we add 3 before 2 because we process:
At the beginning of each level:
len(queue) represents the number of nodes in the current level.
For example:
When processing the second level:
so:
Therefore:
processes exactly:
During this loop, their children may be added to the queue, but those new nodes belong to the next level and will not be processed until the next while iteration.
This is the standard technique for Level Order Traversal.
Because we insert the right child first:
nodes at each level are processed from:
Therefore, the first node:
is the rightmost node of that level.
So:
Consider:
Initially:
Level 1¶
Process:
Since:
add 1:
Add right child first, then left child:
Level 2¶
At the beginning:
The current level contains 2 nodes.
Process 3¶
Since this is the first node:
Add its right child:
Process 2¶
Do not add it to res.
Its right child is 5, so:
After finishing this level:
Level 3¶
Process 4 first:
Therefore:
The answer is:
The important relationship is:
Right child enters queue first
↓
Nodes at next level are ordered right → left
↓
First node of each level is the rightmost node
↓
if i == 0:
record the node
Therefore:
works together with:
There is another common way to solve this problem.
We can insert nodes in the normal order:
Then the last node of every level is the rightmost node.
That would be:
So there are two equivalent strategies:
or:
Your solution uses the first strategy.
Start with root
↓
queue = [root]
↓
Process one level at a time
↓
Store current level size
↓
Pop nodes from queue
↓
First node of level?
↓
Yes → add to res
↓
Add right child
↓
Add left child
↓
Process next level
The key pattern is:
Time Complexity¶
Every node is added to and removed from the queue exactly once:
where n is the number of nodes.
Space Complexity¶
The queue may contain an entire level of the tree:
in the worst case.
More precisely, it is:
where w is the maximum width of the binary tree.
LeetCode 199 is essentially a Level Order Traversal problem.
The special idea in this solution is:
Therefore:
The pattern to remember is:
Study record
| Field | Value |
|---|---|
| Attempts | 1 |
| Mastery | ❚ |
| The Latest Attempt | September 10, 2026 |
Python solution¶
from collections import deque
class Solution:
def rightSideView(self, root: Optional[TreeNode]) -> List[int]:
if not root:
return []
res = []
queue = deque([root])
while queue:
for i in range(len(queue)):
curNode = queue.popleft()
if i == 0:
res.append(curNode.val)
if curNode.right:
queue.append(curNode.right)
if curNode.left:
queue.append(curNode.left)
return res