Spent the first minute overcomplicating it in my head.
Use a depth-first search (DFS) traversal to explore all root-to-leaf paths, maintaining a running sum. When a leaf node is reached, add the current sum to the result list. This approach naturally handles negative values and ensures each path is considered exactly once.
Pro tip: Clarify with the interviewer whether the tree can be empty and what should be returned in that case. Also, mention that you can optimize space by using an iterative approach with a stack if recursion depth is a concern.
Ask about edge cases: empty tree, single node, and whether the tree is balanced. Confirm that a root-to-leaf path must end at a leaf (node with no children).
Decide between recursive DFS (simpler) or iterative DFS/BFS. Explain that DFS is ideal because it naturally tracks the path sum from root to current node.
Write a recursive function that takes a node and the current sum. If the node is a leaf, add the sum to the result. Otherwise, recurse on left and right children with updated sum.
Walk through a small tree with negative values to verify correctness. Check edge cases like empty tree and single node.
State that time complexity is O(N) where N is number of nodes, and space complexity is O(H) for recursion stack, where H is tree height.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.