Given the root of a binary tree and an integer targetSum, return all root-to-leaf paths where each path's sum equals targetSum.
Return the paths in any order; each path lists its values from the root down to the leaf.
[0, 5000].-1000 ≤ Node.val ≤ 1000-1000 ≤ target ≤ 1000DFS with a path list; on a matching leaf, snapshot the path.
Every answer is a root-to-leaf path, and a tree has exactly one such path per leaf. So the job is to visit every leaf, know the path that led there and its sum, and keep the paths whose sum is target. A depth-first walk does that naturally: as it goes down, the nodes from the root to where it stands are exactly the current path.
Time O(n · h)Space O(h)
The full walkthrough and code are part of Premium