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]: The input is small enough that any linear traversal finishes instantly, so the choice between approaches comes down to space usage and code structure, not speed.-100 <= Node.val <= 100: Node values can be negative. This does not affect the traversal logic.Process the tree level by level, scanning each level from left to right. The last node processed at each level is the one visible from the right side, and BFS with a queue produces this order directly.
At each level, we record the queue size before processing, dequeue exactly that many nodes, and add the value of the last one to the result. Children enqueued during a level form the next level, so the size snapshot cleanly separates one level from the next.
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, up to roughly n/2 nodes for a wide tree. The next approach replaces that level-wide queue with a recursion stack whose size is proportional to the tree's height, not its width.
A depth-first traversal that visits the right child before the left child reaches the rightmost node of each level before any other node at that depth. So instead of scanning a whole level and keeping the last node, we record the first node encountered at each new depth.
Detecting a "new" depth requires no extra data structure. Compare the current depth against the size of the result list: if depth == result.size(), no node at this depth has been recorded yet, so the current node is the rightmost one at this level.
The approach is correct 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 finds new depths (4 and 5) and records those left-subtree nodes. They are the only nodes at those depths, so they are the ones 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 child first).depth + 1.dfs(root, 0) and return the result.