Skip to content

Instantly share code, notes, and snippets.

@apiv
Created January 9, 2013 02:15
Show Gist options
  • Save apiv/4489972 to your computer and use it in GitHub Desktop.
Save apiv/4489972 to your computer and use it in GitHub Desktop.
A simple process to measure the depth of a binary tree, using the recursive method
###
Here is a representation of a simple binary tree,
to illustrate the idea of using a self-calling function
to measure the length of a binary tree.
###
nodeMap = [
[
[
[
null,
[
null,
[
null,
[
null,
null
]
]
]
],
[
null,
null
]
],
[
null,
null
]
],
[
null,
[
[
null,
null
],
null
]
]
]
evaluate = (node) ->
if node == null then return 0
left = evaluate node[0]
right = evaluate node[1]
if left > right then left+1 else right+1
depth = evaluate nodeMap
console.log depth
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment