Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b1856f1

Browse files
authored
Create 112-path-sum.js
solved path-sum in JS.
1 parent 3cfb9a5 commit b1856f1

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

javascript/112-path-sum.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// problem link https://leetcode.com/problems/path-sum/
2+
// time complexity O(n) // whatever the number of nodes are.
3+
4+
var hasPathSum = function(root, targetSum) {
5+
6+
const ans = [];
7+
function goDFS(node, curruntSum) {
8+
9+
if(!node) return;
10+
11+
if(!node.left && !node.right) {
12+
ans.push(node.val + curruntSum);
13+
}
14+
15+
goDFS(node.left, curruntSum + node.val);
16+
goDFS(node.right, curruntSum + node.val);
17+
}
18+
goDFS(root, 0);
19+
20+
return ans.includes(targetSum);
21+
};

0 commit comments

Comments
 (0)