Guest User

Untitled

a guest
Jan 21st, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. const hasPathToSum = (node, targetSum) => {
  2. console.log(node, targetSum);
  3. // let currentNode = node
  4. targetSum = targetSum - node.value;
  5. console.log('Target Sum: ', targetSum)
  6. if (node.left) {
  7. console.log("left", targetSum)
  8. hasPathToSum(node.left, targetSum);
  9. }
  10. if (node.right) {
  11. console.log("right", targetSum)
  12. hasPathToSum(node.right, targetSum);
  13. }
  14. if (targetSum = 0) {
  15. console.log('FOUND SUM NODE', node)
  16. return node;
  17. }
  18. return targetSum;
  19. };
  20.  
  21.  
  22.  
  23. let tree = {
  24. value: 5,
  25. left: {
  26. value: 3,
  27. left: {
  28. value: 1,
  29. left: null,
  30. right: null
  31. },
  32. right: {
  33. value: 2,
  34. left: null,
  35. right: null
  36. }
  37. },
  38. right: {
  39. value: 7,
  40. left: null,
  41. right: null
  42. }
  43. }
  44.  
  45.  
  46. let targetSum = 11;
  47.  
  48. hasPathToSum(tree, targetSum);
Add Comment
Please, Sign In to add comment