Watch 10 video solutions for Binary Tree Right Side View, a medium level problem involving Tree, Depth-First Search, Breadth-First Search. This walkthrough by NeetCode has 147,067 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Given the root of a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.
Example 1:
Input: root = [1,2,3,null,5,null,4]
Output: [1,3,4]
Explanation:

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

Example 3:
Input: root = [1,null,3]
Output: [1,3]
Example 4:
Input: root = []
Output: []
Constraints:
[0, 100].-100 <= Node.val <= 100Problem Overview: Given the root of a binary tree, return the list of node values visible when the tree is viewed from the right side. At each depth level, only the rightmost node should appear in the result.
Approach 1: Breadth-First Search (BFS) using Queue (O(n) time, O(n) space)
This approach performs a level-order traversal using a queue. Process the tree level by level and track how many nodes exist at the current depth. As you iterate through nodes in the same level, the last node processed represents the rightmost node visible from that level. Add this node's value to the result list. Each node is enqueued once and dequeued once, which gives O(n) time complexity, while the queue can hold up to one full level of nodes, leading to O(n) space in the worst case.
BFS works well because the problem is naturally defined by levels of a tree. You simply detect the final node encountered in each level iteration. This method is straightforward and easy to implement during interviews, especially when you are already using a queue for level traversal of a binary tree. The algorithm scales cleanly for skewed and balanced trees alike.
Approach 2: Depth-First Search (DFS) Right-First Traversal (O(n) time, O(h) space)
The DFS approach explores the tree recursively while prioritizing the right subtree before the left subtree. Maintain the current depth during recursion and track the maximum depth already recorded in the result list. Whenever you visit a node at a depth equal to the current result size, it means this is the first node encountered at that level from the right side, so you append its value.
This works because visiting the right child first guarantees the first node seen at each depth is the visible one. The recursion stack stores at most the height of the tree, resulting in O(h) space where h is the tree height. In the worst case of a skewed tree, that becomes O(n). DFS is common when working with recursive traversal patterns in depth-first search problems.
Compared to BFS, this method avoids explicitly storing full levels in a queue and instead relies on recursion depth tracking. It integrates naturally with many tree-recursion templates used in tree problems.
Recommended for interviews: Both BFS and DFS run in O(n) time and are accepted solutions. Interviewers often expect the BFS level-order solution first because it directly models "one node per level" visibility. Demonstrating the DFS right-first approach afterward shows deeper understanding of traversal order and recursion patterns.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Breadth-First Search (Queue Level Order) | O(n) | O(n) | Best when reasoning level-by-level in a binary tree and identifying the last node in each level |
| Depth-First Search (Right-First Traversal) | O(n) | O(h) | Useful when using recursive tree traversal templates and tracking depth |