Last Updated: March 29, 2026
We are looking at a binary tree from the right side. For each level of the tree, we want the rightmost visible node. That does not always mean "the right child." If a level has nodes only in the left subtree, the leftmost node at that level is still the one you would see from the right, because nothing blocks it.
So the real question is: for every depth level in the tree, what is the last node when we scan left to right? This reframes the problem from a spatial "standing on the right" visualization to a concrete algorithmic task: find the last node at each level.
This connects directly to level-order traversal. If we process each level left to right, the last node we process at each level is our answer. But we can also solve this with DFS by visiting the right subtree first.
Number of nodes in range [0, 100] — Extremely small input. Even an O(n^2) approach would finish instantly. The focus here is on choosing the right traversal strategy, not on optimizing for performance.-100 <= Node.val <= 100 — Node values can be negative. This does not affect our traversal logic.The most natural way to think about this problem is level by level. If we process all nodes at each level from left to right, the last node we see at each level is the one visible from the right side. This is exactly what BFS does.
We use a queue to process nodes level by level. At each level, we dequeue all the current nodes, and whatever node we dequeue last is the right side view for that level. We just need to keep track of the last value processed in each level's iteration.
levelSize.levelSize nodes from the queue.BFS is already O(n) in time, which is optimal. But the queue holds an entire level at once, which can be up to n/2 nodes for a wide tree. What if we used DFS instead, where the only extra space is the recursion stack (proportional to tree height, not width)?
Instead of processing level by level and picking the last node, what if we always visited the right subtree first? If we do a depth-first traversal where we go right before left, then the first node we encounter at each new depth level is guaranteed to be the rightmost node at that level.
The trick is knowing when we have reached a "new" depth. We can track this by comparing the current depth against the size of our result list. If depth == result.size(), we have not recorded any node at this depth yet, so the current node is the first (rightmost) one we have seen at this level.
This is elegant because it flips the perspective. Instead of scanning each level and keeping the last node, we visit in an order that naturally hits the rightmost node first.
The core principle is that in a right-first DFS, the traversal order guarantees the rightmost node at each level is visited before any other node at the same level. Since we only record the first node we encounter at each new depth, and right-first ordering ensures that first node is the rightmost, we get exactly the right side view.
This works even when the left subtree is deeper than the right subtree. If the right subtree has depth 3 but the left subtree extends to depth 5, levels 4 and 5 only have nodes in the left subtree. When the DFS backtracks to explore the left side, it will find new depths (4 and 5) and record those left-subtree nodes. This is correct because they are the only nodes at those depths, so they are visible from the right.
dfs(node, depth):node is null, return.depth equals the size of result, this is the first node we have seen at this depth. Add node.val to the result.depth + 1 (right first!).depth + 1.dfs(root, 0) and return the result.