I went with BFS and grabbed the last node at each level, which works fine.
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: Clarify with the interviewer whether the tree can be empty or have only one node, and mention that the right-side view includes the rightmost node at each depth even if it's not the right child. Also, discuss trade-offs: BFS uses O(width) space, while DFS uses O(height) space, which can be more efficient for skewed trees.
Confirm that you need to return the rightmost node at each depth, not just nodes that are right children. Clarify edge cases like empty tree or single node.
Decide between BFS and DFS. BFS naturally processes level by level, making it easy to pick the last node. DFS can be more space-efficient for deep trees if you track depth.
For BFS: use a queue, process each level, and add the last node's value to the result. For DFS: traverse right-first, and if the current depth equals the result size, append the node's value.
Walk through a sample tree, including edge cases like a left-skewed tree where the right view includes left children. Verify the output order is top to bottom.
State that both approaches are O(n) time. Discuss space: BFS O(width), DFS O(height). Mention that DFS can be more memory-efficient for skewed trees.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.