Guest User

Untitled

a guest
Jan 21st, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. const hasPathToSum = function(node, targetSum) {
  2. var sums = [];
  3. var add = function(node2, currentValue = 0, lastVal = 0) {
  4. oldCur = currentValue
  5. newCur = currentValue + node2.value
  6. sums.push(newCur)
  7. if(node2.children.length === 0){
  8. newCur = oldCur
  9. }
  10. node2.children.forEach(function(each){
  11. //console.log(newCur)
  12. add(each, newCur, oldCur)
  13. })
  14. }
  15. add(node)
  16. var path = false
  17. for(var i = 0; i < sums.length; i++){
  18. if(sums[i] === targetSum){
  19. path = true
  20. }
  21. }
  22. return path
  23. };
  24.  
  25. //tree needs a .value, and .children
Add Comment
Please, Sign In to add comment