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

5 Secure Coding Tips in Go

ETL Using Python and Pandas

presentable White CZ Gold Plated White Ring supply US 6789

A plea against anonymous functions in streams

Depth-First Search: A Primer

How to Print Alphabet E in Python?

Travis CI script for your Swagger/OpenAPI Specification

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

Keeping It ‘Real’ And ‘Clean’ is Realme’s Secret Sauce For Winning Indian Heart

The Spume coin is bscchain based coin the upcoming time they will integrate with more chains to…

What happens when you type gcc main.c

Hong Kong’s CBDC strategy: What we know so far