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 Tree:
5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1
Target Sum: 22 Result: true (path 5 -> 4 -> 11 -> 2 = 22)
Key Insight: Use DFS to explore all root-to-leaf paths. At each step, subtract the current node's value from the target. If we reach a leaf node where the remaining target equals the node's value, we found a valid path.
root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22trueroot = [1,2,3], targetSum = 5falseroot = [], targetSum = 0falseThe number of nodes in the tree is in the range [0, 5000].-1000 <= Node.val <= 1000-1000 <= targetSum <= 1000Click "Run" to execute your code against test cases
Socratic guidance - I'll ask questions, not give answers