CoDEVIANT #24 — Apples Falling from the Binary Tree

Mmmm…sweet, candy red apples…
You can visit any apple-tree (node), as often as you like, but you can’t pick up an apple that is already retrieved.
class BinaryTree {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
function nodeDepths(root) {
let depthCount = 0
return helper(root, depthCount)
}
function helper(node, counter) {
if(!node) {
return 0
}
let numbah = counter + helper(node.right, counter + 1) + helper(node.left, counter + 1)
return numbah
}
class BinaryTree {
constructor(value) {
this.value = value
this.left = null
this.right = null
}
}
function nodeDepths(root) {
let depthCount = 0
return helper(root, depthCount)
}
function helper(node, counter) {
if(!node) {
return 0
}
let numbah = counter + helper(node.right, counter + 1) + helper(node.left, counter + 1)
return numbah
}
Yay recursion!
How’d you like dem apples?

--

--

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)