Root-to-leaf path sum
Easytreedfsrecursion
Return true if some path from the root to a leaf (a node with no children) adds up to target.
Examples write a tree level by level, with null for a missing child: [3,9,20,null,null,15,7].
Example 1
Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1], target = 22 Output: true
5 + 4 + 11 + 2 = 22.
Example 2
Input: root = [1,2,3], target = 5 Output: false
Constraints
- 0 ≤ number of nodes ≤ 5000
- -1000 ≤ Node.val ≤ 1000
- -1000 ≤ target ≤ 1000
Submit checks your solution against 9 tests, including hidden ones.
// Provided by the judge: keep these definitions
class TreeNode {
constructor(val = 0, left = null, right = null) {
this.val = val
this.left = left
this.right = right
}
}
/**
* @param {TreeNode|null} root
* @param {int} target
* @return {bool}
*/
function hasPathSum(root, target) {
}