-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpathSum.js
More file actions
46 lines (39 loc) · 1.39 KB
/
Copy pathpathSum.js
File metadata and controls
46 lines (39 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
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 1:
Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22
Output: true
Explanation: The root-to-leaf path with the target sum is shown: 5 → 4 → 11 → 2
Example 2:
Input: root = [1,2,3], targetSum = 5
Output: false
Example 3:
Input: root = [], targetSum = 0
Output: false
Constraints:
The number of nodes in the tree is in the range [0, 5000].
-1000 <= Node.val <= 1000
-1000 <= targetSum <= 1000
*/
/**
* @param {TreeNode} root
* @param {number} targetSum
* @return {boolean}
*/
function hasPathSum(root, targetSum) {
if (!root) return false;
if (!root.left && !root.right) return root.val === targetSum;
return hasPathSum(root.left, targetSum - root.val) ||
hasPathSum(root.right, targetSum - root.val);
}
// Helper
function TreeNode(val, left, right) { this.val = val; this.left = left || null; this.right = right || null; }
// Example usage:
const root = new TreeNode(5,
new TreeNode(4, new TreeNode(11, new TreeNode(7), new TreeNode(2)), null),
new TreeNode(8, new TreeNode(13), new TreeNode(4, null, new TreeNode(1)))
);
console.log(hasPathSum(root, 22)); // Output: true
console.log(hasPathSum(root, 5)); // Output: false