Created
June 22, 2017 16:52
-
-
Save betterkenly/2fe0f1c6f8e74d8aa995fd07ebc543b9 to your computer and use it in GitHub Desktop.
hasPathToSum
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const hasPathToSum = function(node, targetSum) { | |
// your code here | |
// let search = (node, val) => { | |
// if (node.value === null) { | |
// return; | |
// } | |
// if (val === targetSum) { | |
// return true; | |
// } | |
// if (val > targetSum) { | |
// return; | |
// } | |
// val += node.value; | |
// if(node.left) { | |
// search(node.left, sum); | |
// } | |
// if(node.right) { | |
// search(node.right, sum); | |
// } | |
// if(!node.right && !node.left) { | |
// return; | |
// } | |
// } | |
// return search(node, 0) === true? true: false ; | |
// your code here | |
// let search = (node, val) => { | |
if (node.value === null) { | |
return false; | |
} | |
if (node.value === targetSum && (node.left === null && node.right === null)) { | |
return true; | |
} | |
return hasPathToSum(node.left, targetSum - node.value) || hasPathToSum(node.right, targetSum - node.value); | |
// } | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment