Two paths here and I fumbled around deciding which to go with.
Use a level-order traversal (BFS) with a queue, and for each level, record the last node's value. Alternatively, use DFS with depth tracking, updating the result array at each depth with the current node's value. Both approaches yield O(n) time and O(n) space, but BFS is more intuitive for this problem.
Pro tip: Mention that the right-side view is essentially the last node at each depth, and clarify that it's not just the rightmost path—nodes from the left subtree can be visible if they extend deeper. This shows you understand the subtlety beyond the naive solution.
Confirm that the tree can be empty, nodes can have only left or right children, and the output should be a list of values from top to bottom. Discuss examples to ensure alignment.
Decide between BFS (level-order) and DFS (pre-order with depth tracking). Explain why BFS is straightforward: process each level and take the last node.
For BFS: use a queue, iterate level by level, and for each level, record the value of the last node. For DFS: traverse right-first, and update the result array at each depth if it's the first time visiting that depth.
State that both approaches run in O(n) time and O(n) space (for the queue or recursion stack), where n is the number of nodes.
Walk through a sample tree, including edge cases like a skewed tree or a tree where the left subtree is deeper, to verify the solution.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.