-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath-sum.js
More file actions
41 lines (37 loc) · 812 Bytes
/
path-sum.js
File metadata and controls
41 lines (37 loc) · 812 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
* 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) return false
var s = 0
var result = false
function me(node) {
if (!node.left && !node.right) {
s += node.val
if (s === sum) result = true
s -= node.val
return
}
if (node.left) {
s += node.val
me(node.left)
s -= node.val
}
if (node.right) {
s += node.val
me(node.right)
s -= node.val
}
}
me(root)
return result
};