230. Binary Tree Paths
You are given the root of a binary tree with at least one node. A leaf is a node with no children, and a root-to-leaf path is the sequence of node values you pass through while walking from the root down to one leaf.
Produce one string per leaf: the values along its path joined by -> (for example 1->2->5). Return the list of all such path strings.
To make the answer unambiguous, emit the paths in the order a depth-first walk that always visits a left child before its right sibling reaches their leaves.
Example 1:
Input: root = [1, 2, 3, null, 5]
Output: ["1->2->5", "1->3"]
Explanation: The tree has two leaves, 5 and 3. Walking left first reaches 5 via 1 -> 2 -> 5, then 3 via 1 -> 3.
Example 2:
Input: root = [7]
Output: ["7"]
Explanation: A single node is both root and leaf, so the only path is just "7".
Constraints:
- 1 ≤ number of nodes ≤ 100
- -100 ≤ Node.val ≤ 100
Hints:
Carry the values seen so far while walking down. The moment you stand on a node with no left and no right child, the carried sequence is a complete answer.
Recursion handles the bookkeeping: append the current value, recurse into whichever children exist, then pop the value on the way back up (classic backtracking). Alternatively, pass an immutable string down so nothing needs undoing.
▶ Run checks these sample cases. Submit also runs hidden edge cases.
Input: root = [1, 2, 3, null, 5]
Expected output: ["1->2->5", "1->3"]