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 3.10 KB | None | 0 0
  1. //given a BST with integer node values, find the path to a target sum.
  2. //Assumptions:
  3. //Inputs: a start node and a target value
  4. //Outputs: boolean
  5. //Methodology: since negative numbers change things, A full tree traversal is required.
  6. //I will go with depth first since this will be easier to trace routes
  7.  
  8. class Node {
  9. constructor(value) {
  10. //the node value (integers only here)
  11. this.value = value;
  12. //left and right are references to other nodes
  13. //left = lt, right = gt
  14. this.left = null;
  15. this.right = null;
  16. }
  17. addChild(value) {
  18. //add a child to the tree
  19. if (value < this.value) {
  20. if (this.left) {
  21. this.left.addChild(value);
  22. }
  23. else {
  24. this.left = new Node(value);
  25. }
  26. }
  27. else if (value > this.value) {
  28. if (this.right) {
  29. this.right.addChild(value);
  30. }
  31. else {
  32. this.right = new Node(value);
  33. }
  34. }
  35. }
  36. }
  37.  
  38. const hasPathToSum = (node, targetSum) => {
  39. targetSum -= node.value;
  40. if (targetSum === 0) return true;
  41. if (node.left) {
  42. let result = hasPathToSum(node.left, targetSum);
  43. if (result) {
  44. return true;
  45. }
  46. }
  47. if (node.right) {
  48. let result = hasPathToSum(node.right, targetSum);
  49. if (result) {
  50. return true;
  51. }
  52. }
  53. return false; //If we made it here, there was no path to the target sum();
  54. };
  55.  
  56. //test
  57. //one
  58. // 10
  59. // 6 12
  60. // 3 9 11 13
  61.  
  62.  
  63. // let treeOne = new Node(10);
  64. // treeOne.addChild(6);
  65. // treeOne.addChild(12);
  66. // treeOne.addChild(3);
  67. // treeOne.addChild(9);
  68. // treeOne.addChild(11);
  69. // treeOne.addChild(13);
  70. // console.log('testing tree one');
  71. // console.log(hasPathToSum(treeOne, 35)); //true
  72. // console.log(hasPathToSum(treeOne, 19)); //true
  73. // console.log(hasPathToSum(treeOne, 25)); //true
  74. // console.log(hasPathToSum(treeOne, 22)); //true
  75. // console.log(hasPathToSum(treeOne, 16)); //true
  76. // console.log(hasPathToSum(treeOne, 33)); //true
  77. // console.log(hasPathToSum(treeOne, 10)); //true
  78. // console.log(hasPathToSum(treeOne, 21)); //false
  79. // console.log(hasPathToSum(treeOne, -10)); //false
  80. // console.log(hasPathToSum(treeOne, 100)); //false
  81. // console.log(hasPathToSum(treeOne, 0)); //false
  82. // console.log();
  83.  
  84.  
  85. //test
  86. //two
  87. // 20
  88. // 10 30
  89. // -9 15 25 40
  90. // -100 -5
  91.  
  92. // let treeTwo = new Node(20);
  93. // treeTwo.addChild(10);
  94. // treeTwo.addChild(30);
  95. // treeTwo.addChild(-9);
  96. // treeTwo.addChild(15);
  97. // treeTwo.addChild(25);
  98. // treeTwo.addChild(40);
  99. // treeTwo.addChild(-100);
  100. // treeTwo.addChild(-5);
  101. // console.log('testing tree two');
  102. // console.log(hasPathToSum(treeTwo, 20)); //true
  103. // console.log(hasPathToSum(treeTwo, 50)); //true
  104. // console.log(hasPathToSum(treeTwo, 90)); //true
  105. // console.log(hasPathToSum(treeTwo, 75)); //true
  106. // console.log(hasPathToSum(treeTwo, 30)); //true
  107. // console.log(hasPathToSum(treeTwo, 45)); //true
  108. // console.log(hasPathToSum(treeTwo, 21)); //true
  109. // console.log(hasPathToSum(treeTwo, 16)); //true
  110. // console.log(hasPathToSum(treeTwo, -79)); //true
  111. // console.log(hasPathToSum(treeTwo, -100)); //false
  112. // console.log(hasPathToSum(treeTwo, 100)); //false
  113. // console.log(hasPathToSum(treeTwo, 0)); //false
  114.  
  115.  
  116. //all tests pass
Add Comment
Please, Sign In to add comment