I went with BFS and grabbed the last node at each level, which works cleanly.
Use BFS level-order traversal, recording the last node at each level for the right view. For the follow-up, adapt the traversal to record the first node for the left view or handle missing children by falling back to the leftmost node.
Pro tip: Clarify the follow-up requirements upfront and discuss trade-offs between BFS and DFS; mentioning that BFS naturally handles level boundaries and is easier to extend for variants shows depth.
Confirm the definition of 'right side view' and discuss edge cases like empty tree, single node, and skewed trees. Ask about the follow-up variant to understand expectations.
Decide between BFS and DFS. BFS is straightforward for level-order processing; DFS can also work by tracking depth and updating the view when visiting a node at a new depth from the right first.
For BFS: use a queue, process each level, and record the last node's value. For DFS: traverse right subtree first, and if the current depth equals the result size, append the node's value.
For left view, record the first node at each level in BFS or traverse left first in DFS. For fallback to leftmost when no right child, modify the traversal to consider the left child if the right is absent.
State time and space complexity: O(N) time, O(W) space for BFS where W is max width, O(H) for DFS. Walk through a small example to verify correctness.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.