Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1458 from aadil42/patch-13
Browse files Browse the repository at this point in the history
Create 112-path-sum.js
  • Loading branch information
Ahmad-A0 authored Dec 22, 2022
2 parents 7b388db + 6b01f9f commit 66f73a5
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions javascript/112-Path-Sum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// problem link https://leetcode.com/problems/path-sum/
// time complexity O(n) // whatever the number of nodes are.

var hasPathSum = function(root, targetSum) {

const ans = [];
function goDFS(node, curruntSum) {

if(!node) return;

if(!node.left && !node.right) {
ans.push(node.val + curruntSum);
}

goDFS(node.left, curruntSum + node.val);
goDFS(node.right, curruntSum + node.val);
}
goDFS(root, 0);

return ans.includes(targetSum);
};

0 comments on commit 66f73a5

Please sign in to comment.