Guest User

Untitled

a guest
Jan 21st, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. const hasPathToSum = function(node, targetSum) {
  2. var wasFound = false;
  3. var getBranchSum = function(node, sum = 0) {
  4. sum += node.value;
  5. if (node.left) {
  6. getBranchSum(node.left, sum);
  7. }
  8. if (node.right) {
  9. getBranchSum(node.right, sum);
  10. }
  11. if (!node.left && !node.right) {
  12. if (sum === targetSum) {
  13. wasFound = true;
  14. }
  15. }
  16. };
  17. getBranchSum(node);
  18. return wasFound;
  19. };
  20.  
  21. const hasPathToSum = function(node, targetSum) {
  22. var allBranchSums = [];
  23. var getBranchSum = (node, sum=0) => {
  24. sum += node.value;
  25. if (node.left) {
  26. getBranchSum(node.left, sum);
  27. }
  28. if (node.right) {
  29. getBranchSum(node.right, sum);
  30. }
  31. if (!node.left && !node.right) {
  32. allBranchSums.push(sum);
  33. }
  34. };
  35. getBranchSum(node);
  36. return allBranchSums.includes(targetSum);
  37. };
Add Comment
Please, Sign In to add comment