A root-to-leaf path starts at the root and follows child links until it reaches a leaf (a node with no children).
A park stores its trail map as a binary tree. Each node is one stretch of trail and holds its height change in metres (negative for a descent); the root is the stretch leaving the car park, and every leaf is a viewpoint where a route ends. Write has_path_sum(root, target) that returns True when some route from the root to a leaf has height changes adding up to exactly target, and False otherwise. An empty map has no routes.
Examples
10
/ \
-3 6
/ \ \
8 2 -4
/ / \
1 5 9
Input: root = [10, -3, 6, 8, 2, None, -4, 1, None, None, None, 5, 9], target = 17
Output: True
Explanation: 10 -> 6 -> -4 -> 5 sums to 17.
4
/ \
1 6
Input: root = [4, 1, 6], target = 7
Output: False
Explanation: The two routes sum to 5 and 10.
Input: root = [3, 4], target = 3
Output: False
Explanation: Node 3 is not a leaf; the only route is 3 -> 4 with sum 7.
Constraints
0 <= number of nodes <= 1000- Node values and
targetmay be negative.
Goals
- Pass information downwards through recursive calls (the remaining target)
- Detect a leaf node correctly
- Combine boolean results from two subtrees with or