I knew it was a DFS path problem the second I read it, but the indentation requirement made me second-guess my approach for a minute.
Use a depth-first search (DFS) traversal that tracks the current path and column offset for each node. When a leaf is reached, print the path with each node indented by its column offset relative to the root. Handle negative offsets by shifting all indentation by the minimum offset encountered.
Pro tip: Clarify with the interviewer whether the indentation should be relative to the root (column 0) or normalized to avoid negative spaces. Mention that you can compute the minimum column offset during traversal and shift all offsets accordingly, showing attention to edge cases.
Confirm that each node's column is determined by its position relative to the root: left child decreases column by 1, right child increases by 1. Ask about output format and whether negative columns are allowed.
Select DFS (recursive or iterative) to explore all root-to-leaf paths while maintaining the current path and column offset. Explain why DFS is suitable for path-based problems.
During traversal, maintain a list of nodes in the current path and their column offsets. When a leaf is reached, record the path and offsets for printing.
After collecting all paths, find the minimum column offset across all nodes to shift offsets so that the leftmost node starts at column 0. Alternatively, compute the shift on the fly if possible.
For each root-to-leaf path, print each node's value preceded by spaces equal to its normalized column offset. Ensure proper alignment and separation between paths.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.