My first instinct was to just do a BFS and track the first and last node at each level, which is roughly right, but I kept fumbling the dedup condition.
Clarify the definition of 'outer boundary path' and edge cases, then propose a two-phase traversal: first find the deepest leftmost node and collect the leftmost path from root to that node, then collect the rightmost path from root to the deepest rightmost node. Combine the leftmost path (excluding the root) with the reversed rightmost path (excluding the root) to form the final sequence, ensuring no duplicates when leftmost and rightmost nodes coincide at a depth.
Pro tip: Demonstrate maturity by explicitly discussing edge cases (single node, skewed tree, duplicate values) and analyzing time/space complexity. Also, mention that you would confirm the exact ordering with the interviewer before coding, as interpretations may vary.
Restate the problem in your own words and ask clarifying questions about edge cases, such as what to do when the tree has only one node or when leftmost and rightmost paths overlap.
Determine the leftmost path from the root to the deepest leftmost node and the rightmost path from the root to the deepest rightmost node. Note that the deepest leftmost node is found by always taking the first child at each level until a leaf is reached.
Traverse the tree to collect the leftmost path (including the deepest leftmost node but excluding the root) and the rightmost path (including the deepest rightmost node but excluding the root). Use DFS or BFS with appropriate child ordering.
Concatenate the leftmost path with the reversed rightmost path. If the leftmost and rightmost nodes at the same depth are identical, include that node only once (typically in the leftmost path).
State the time complexity (O(N) where N is the number of nodes) and space complexity (O(H) for recursion stack or O(N) for storing paths), and discuss potential optimizations.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.