We are asked to find all root-to-leaf paths in a binary tree and return them as strings.
- A path is defined as starting from the root and ending at a leaf node.
- Each path should be represented in the format:
- root->node1->node2->...->leaf
Key insights:
- This is a DFS traversal problem.
- At each node, we build the current path string.
- When reaching a leaf node, we push the complete path into the result list.
- Start DFS from the root, initialize path =
to_string(root->val). - At each node:
- If it’s a leaf (no left & no right):
- Push the current
pathinto the result.
- Push the current
- Else:
- Recurse left with updated path =
path + "->" + left->val. - Recurse right with updated path =
path + "->" + right->val.
- Recurse left with updated path =
- Continue until all leaves are reached.
- Base case: if node is
NULL, just return. - Leaf detection:
!root->left && !root->right. - We pass path by value (string copy) at each recursive call, which is safe and avoids manual backtracking.
- DFS naturally ensures that all root-to-leaf paths are explored.
- Time:
O(n) - Each node is visited exactly once.
- Space:
O(h)recursion stack (wherehis the height of the tree).- Extra
O(n)for storing result strings.