-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPathSum.js
32 lines (29 loc) · 890 Bytes
/
PathSum.js
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
//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.
//Runtime Complexity
//Linear, O(n).
//Memory Complexity
//Logarithmic, O(log n).
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @param {number} targetSum
* @return {boolean}
*/
var hasPathSum = function(root, targetSum) {
if (!root) {
return false;
}
else if(!(root.left || root.right)) {
return root.val === targetSum;
}
else {
return hasPathSum(root.left, targetSum-root.val) || hasPathSum(root.right, targetSum-root.val)
}
};