CoDEVIANT #23 — Tree Hee Hee

This….THIS is the energy.
Not those kinds of trees.
class BinaryTree {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
function branchSums(root) {
let answer = []
helper(root, 0, answer)
return answer
}
function helper(node, currentSum, array) {
if(!node) {
return
}

const val = currentSum + node.value
if(!node.left && !node.right) {
array.push(val);
return;
}

helper(node.left, val, array)
helper(node.right, val, array)

}
class BinaryTree {
constructor(value) {
this.value = value
this.left = null
this.right = null
}
}
function branchSums(root) {
let answer = []
helper(root, 0, answer)
return answer
}
function helper(node, currentSum, array){
if(!node) {
return
}
const value = currentSum + node.value
if(!node.left && !node.right) {
array.push(value)
return
}
helper(node.left, val, array)
helper(node.right, val, array)
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store