Path Sum
EasyP2Coding
Given the root of a binary tree and an integer targetSum, return true if there is a root-to-leaf path whose node values add up to targetSum. A leaf is a node with no children. An empty tree has no paths.
Example 1
- 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.
Example 2
- Input
- root = [1,2,3], targetSum = 5
- Output
- false
Example 3
- Input
- root = [], targetSum = 0
- Output
- false
Constraints
- 0 <= number of nodes <= 5000
- -1000 <= Node.val <= 1000
- -1000 <= targetSum <= 1000
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 9. ⌘Enter runs, ⌘⇧Enter submits.