CoDEVIANT #15 (10/2/20) — The Spookiest Halloween Yet…

  • This will likely NOT get you the job and will get you in jail, where you have no choice BUT to fight.
…unless you wanna end up somebody’s bitch.
Sometimes, that’s just how it is, chief.
treeNode = {
this.value = 10,
this.left = {
value: 5,
left: { value: 2, left: [BinaryTree], right: null },
right: { value: 5, left: null, right: null }
},
this.right = {
value: 15,
left: { value: 13, left: null, right:[BinaryTree] },
right: { value: 22, left: null, right: null }
}
}
  • tree
  • target
  • closest
  • tree / tree
  • target / 16
  • closest / tree.value (in this case at the top it is 10)
if(tree == null) {
return closest
}
If there’s no tree, there’s nothing for me.
if(Math.abs(target - closest) > Math.abs(target - tree.value)) {
closest = tree.value
}
if(target < tree.value) {
return snoopsTreeFriend(tree.left, target, closest)
} else if (target > tree.value) {
return snoopsTreeFriend(tree.right, target, closest)
} else {
return closest
}
It’s recursion time!
function snoopsTreeFriend(tree, target, closest) {
if(tree == null) {
return closest
}
if(Math.abs(target - closest) > Math.abs(target - tree.value)) {
closest = tree.value
}
if(target < tree.value) {
return snoopsTreeFriend(tree.left, target, closest)
} else if (target > tree.value) {
return snoopsTreeFriend(tree.right, target, closest)
} else {
return closest
}
}
Slight exaggeration of me running away to do something frivolous like playing video games or doomscrolling while having Netflix play on my phone.

--

--

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)