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.79 KB | None | 0 0
  1. class binaryTree {
  2. constructor(val) {
  3. this.val = val;
  4. this.left = null;
  5. this.right = null;
  6. }
  7. addLeft(val) {
  8. this.left = new binaryTree(val);
  9. return this.left;
  10. }
  11. addRight(val) {
  12. this.right = new binaryTree(val);
  13. return this.right;
  14. }
  15. }
  16.  
  17. const hasPathToSum = (node, targetSum, targetLeft = targetSum) => {
  18. const currLeft = targetLeft - node.val;
  19. if (!node.left && !node.right) {
  20. if (currLeft === 0) {
  21. return true;
  22. }
  23. return false;
  24. }
  25.  
  26. let leftPath = false;
  27. let rightPath = false;
  28.  
  29. if (node.left && currLeft < node.val) {
  30. leftPath = hasPathToSum(node.left, targetSum, currLeft);
  31. }
  32. if (node.right && currLeft > node.val) {
  33. rightPath = hasPathToSum(node.right, targetSum, currLeft);
  34. }
  35.  
  36. return false || leftPath || rightPath;
  37. };
Add Comment
Please, Sign In to add comment