Skip to content

Instantly share code, notes, and snippets.

@betterkenly
Created June 22, 2017 16:52
Show Gist options
  • Save betterkenly/2fe0f1c6f8e74d8aa995fd07ebc543b9 to your computer and use it in GitHub Desktop.
Save betterkenly/2fe0f1c6f8e74d8aa995fd07ebc543b9 to your computer and use it in GitHub Desktop.
hasPathToSum
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