Given the root of a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that adding up all the values along the path equals targetSum. A leaf is a node with no children.
Example 1
Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22
Output: true
Explanation: The root-to-leaf path 5 → 4 → 11 → 2 adds up to 5 + 4 + 11 + 2 = 22.
Example 2
Input: root = [1,2,3], targetSum = 5
Output: false
Explanation: The two root-to-leaf paths sum to 1 + 2 = 3 and 1 + 3 = 4. Neither equals 5.
Example 3
Input: root = [1,2], targetSum = 1
Output: false
Explanation: The root 1 is not a leaf because it has a child, so the path must continue to 2. The only root-to-leaf sum is 3, not 1.
Walk down from the root, subtracting each node's value from the remaining target. At a leaf, the path sums to the target exactly when the remaining amount equals the leaf's value. Any leaf that closes the gap makes the answer true.
Run checks all cases above. Submit evaluates all test cases.