CoDEVIANT #23 — Tree Hee Hee

This….THIS is the energy.
Not those kinds of trees.
  • root: the top node of the tree
  • leaf node: a node that has no children
  • branch: the traversal from root to a leaf-node following a path of links between nodes
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
}
  • define an empty array, answer
  • call some method named helper and pass in the root, the number zero, and the answer array we just defined.
  • we 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)
}
  • We call helper and pass in the root, 0, and answer for the arguments node, currentSum, array (respectively)
  • Once inside the machinations of the helper method we check to see if the node exists. If it does not, then we return.
  • So if the node does exist, then we create a variable called value and we make it equal the currentSum (on the first pass its 0) plus node.value. ***If we’re using the binary tree illustration from earlier in this article it’s going to be 1***
  • Next we make sure that we have children nodes that regards the current node as their parent. If this is not the case, we push value into the array and return
  • However, if this node does have children, then we call helper and pass in the left and right child in the first argument slot. value will be the 2nd argument, and array will be the third.
  • Adding 0 and 1 (the root node’s value)
  • Calling itself on the node’s left child — and technically waiting for that to resolve itself (which will take the blink of an eye). This is where the method will be calculating the following before pushing them into our answer array and then returning that:
    1–3–2–7: 13
    1–3–2–3: 9
    1–3–5–4: 13
    1–3–5–9: 18
  • Calling itself on the node’s right child — you know the drill:
    1–4–2–3: 10
    1–4–2–4: 11
    1–4–6–8: 19
    1–4–6–9: 20

--

--

--

is a web developer, opera singer, actor, and lover of cats. (adrian-rosales.tech)

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Product Update 10.31.21: Our Favorite Guides to Great Code Reviews

NodeBots Advice Part I: General Advice for the Aspiring NodeBotter

Go Context Explained

Packages and data visualizations with R

Zendesk setup — How to escalate to pager duty when an SLA is breached

Five Lessons From 3,650 Days of Web Performance Work

Beginner's Guide to Writing your First Linux patch -Make Linux Kernel Better

Safari, CSS Grid, Tables, and Quirks Mode

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
Adrian Rosales

Adrian Rosales

is a web developer, opera singer, actor, and lover of cats. (adrian-rosales.tech)

More from Medium

The Monkey of Anarkey Project

✨📖// Is the Bible True? //📖✨

All Time Reward Distribution System for CommunoCAPITAL Members:

What the Alnair Project lacks? / Ideas for developing the Alnair Project?