Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const hasPathToSum = function(node, targetSum) {
- var sums = [];
- var add = function(node2, currentValue = 0, lastVal = 0) {
- oldCur = currentValue
- newCur = currentValue + node2.value
- sums.push(newCur)
- if(node2.children.length === 0){
- newCur = oldCur
- }
- node2.children.forEach(function(each){
- //console.log(newCur)
- add(each, newCur, oldCur)
- })
- }
- add(node)
- var path = false
- for(var i = 0; i < sums.length; i++){
- if(sums[i] === targetSum){
- path = true
- }
- }
- return path
- };
- //tree needs a .value, and .children
Add Comment
Please, Sign In to add comment