Path Sum
Given the root of a binary tree and an integer targetSum, return true if the tree has at least one root-to-leaf path such that the sum of the node values along the path equals targetSum. A leaf is a node with no children.
Open official problem prompt ↗Decide whether some path from the root down to a leaf has node values that add up exactly to targetSum.
You start a hike at the summit (root) with a fuel budget. Each trail marker (node) burns some fuel. You succeed only if you arrive at a trail end (leaf) with exactly zero fuel left.
- Input
- root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22
- Output
- true
- Why
- The path 5 -> 4 -> 11 -> 2 sums to 22.
The number of nodes is in the range [0, 5000]-1000 <= Node.val <= 1000-1000 <= targetSum <= 1000