Binary Tree Right Side View: LeetCode 199
Binary Tree Right Side View (LeetCode 199) is one of the most frequently asked binary tree problems at companies like Meta and Amazon. The problem sounds simple — imagine standing on the right side of a tree and list the nodes you can see — but it tests whether you truly understand level-order traversal and how to extract specific nodes from each level.
At its core, this is a level-order BFS problem with a twist. Instead of collecting every node at each level, you only keep the last one. That single change transforms a standard BFS template into a targeted extraction pattern that appears in dozens of tree problems.
In this walkthrough, you will learn both the BFS and DFS approaches to solve binary tree right side view on LeetCode, understand the visual intuition behind each, and see how the same technique extends to left side view, bottom view, and top view variants.
Understanding the Right Side View Problem
Given the root of a binary tree, return the values of the nodes you can see when looking at the tree from the right side, ordered from top to bottom. In other words, for each level of the tree, you want the rightmost node.
Consider a tree with root 1, left child 2, and right child 3. Node 2 has a right child 5, and node 3 has a right child 4. Looking from the right, you see nodes 1, 3, and 4. Node 2 is hidden behind node 3, and node 5 is hidden behind node 4.
The key insight is that "visible from the right" simply means the last node at each depth level. This reframing turns a spatial visualization problem into a straightforward level-order traversal problem where you track the rightmost node each level produces.
Interview Favorite
Binary Tree Right Side View (#199) is a favorite at Meta and Amazon — it's the level-order variant that tests whether you can adapt BFS to extract specific nodes per level.
BFS Approach: Right Side View with Level-Order Traversal
The BFS approach is the most intuitive way to solve binary tree right side view on LeetCode. You perform a standard level-order traversal using a queue, and for each level, you record the last node processed. That last node is the rightmost one visible from the right side.
Start by pushing the root into a queue. For each level, record how many nodes are in the queue — that is the level size. Process all nodes at the current level left to right, enqueueing their children. The final node you process at each level is the answer for that depth.
This approach runs in O(n) time and O(w) space where w is the maximum width of the tree. In the worst case of a complete binary tree, w is roughly n/2, so space is O(n). For most interview trees, this is perfectly efficient.
- Initialize a queue with the root node and an empty result array
- While the queue is not empty, record the current level size
- Process all nodes at this level — for each node, enqueue its left then right child
- The last node processed in the level loop is the rightmost — add its value to the result
- Return the result array after all levels are processed
DFS Approach: Modified Preorder Traversal
The DFS approach offers an elegant alternative. Instead of processing level by level, you traverse the tree depth-first, visiting the right child before the left. Track the maximum depth you have seen so far. Each time you reach a new depth for the first time, that node is the rightmost at its level.
The traversal order is root, right, left — a modified preorder. Because you visit right before left, the first node encountered at any new depth is guaranteed to be the rightmost node at that level. You simply check if the current depth equals the length of your result array to determine if this is a new level.
DFS uses O(h) space where h is the height of the tree, which can be better than BFS for tall, narrow trees. For balanced trees, h is log(n). For skewed trees, h is n, matching the worst case of BFS.
- Define a recursive function taking the current node and depth
- If the node is null, return immediately
- If depth equals the result array length, this is a new level — append the node value
- Recurse on the right child first with depth + 1
- Recurse on the left child with depth + 1
Watch Out
The DFS approach visits right child BEFORE left — this ensures the first node seen at each new depth is the rightmost. If you visit left first, you need extra logic.
Visual Walkthrough of Right Side View
Let us walk through the example tree [1,2,3,null,5,null,4] step by step using the BFS approach. The tree has root 1, left child 2 with right child 5, and right child 3 with right child 4.
Level 0 has one node: 1. The last node is 1, so the right side view starts with [1]. Level 1 has two nodes: 2 and 3. Processing left to right, the last node is 3. Result becomes [1, 3]. Level 2 has two nodes: 5 and 4. The last node is 4. Final result is [1, 3, 4].
With the DFS approach on the same tree, you visit 1 (depth 0, new level, add 1), then 3 (depth 1, new level, add 3), then 4 (depth 2, new level, add 4), then back up to visit 2 (depth 1, already seen), then 5 (depth 2, already seen). The result is the same: [1, 3, 4].
Edge Cases to Handle
Empty tree is the simplest edge case — if root is null, return an empty array. A single node tree returns just that node value. Both approaches handle these naturally without special-casing.
A tree with all left children is a subtle case. Consider the tree [1,2,null,3,null,4]. Every node is a left child, so the right side view at each level is the only node at that level: [1, 2, 3, 4]. The BFS approach handles this automatically because the last node at each level is also the only node.
A tree with all right children is even simpler — every right child is visible. A perfectly balanced tree shows only the rightmost path. Understanding these cases builds your intuition for how the algorithm adapts to different tree shapes.
- Empty tree: return []
- Single node: return [root.val]
- All left children: every node is visible since each level has exactly one node
- All right children: the entire right spine is the answer
- Complete binary tree: only the rightmost node at each level appears
Pro Tip
In BFS level-order, the last node processed in each level is the rightmost — simply overwrite your level result as you process nodes, and the final value is the answer for that level.
What Binary Tree Right Side View Teaches You
LeetCode 199 is more than a single problem — it is a gateway to an entire family of BFS level-order variants. Once you understand how to extract the rightmost node at each level, you can adapt the same pattern to find the leftmost node (left side view), the last node at the deepest level (bottom-left value), or even the top and bottom views of a binary tree.
The BFS template of processing nodes level by level with a queue and tracking level size is one of the most versatile patterns in tree problems. Problems like Binary Tree Level Order Traversal (LeetCode 102), Binary Tree Zigzag Level Order Traversal (LeetCode 103), and Average of Levels (LeetCode 637) all share this foundation.
Practice recognizing when a problem asks you to operate on individual tree levels. That recognition immediately points you to BFS with level size tracking. Drill this pattern with YeetCode flashcards until the BFS template is second nature, and these level-order variants become quick solves in interviews.