-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.js
More file actions
25 lines (25 loc) · 738 Bytes
/
solution.js
File metadata and controls
25 lines (25 loc) · 738 Bytes
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
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @param {number} sum
* @return {boolean}
*/
var hasPathSum = function(root, sum) {
if (root === null) {
return false
} else if (root.left === null && root.right === null) {
return root.val === sum
} else if (root.left !== null && root.right === null) {
return hasPathSum(root.left, sum - root.val)
} else if (root.left === null && root.right !== null) {
return hasPathSum(root.right, sum - root.val)
} else {
return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val)
}
};