Created
August 4, 2017 03:50
-
-
Save hamannjames/4e891ba16bff12404dbcf99378122913 to your computer and use it in GitHub Desktop.
Get largest value in tree depth using depth first search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function largestValuesInTreeRows(t) { | |
function depthSearch(current) { | |
let values = []; | |
let depth = 0; | |
function traverse(current, depth) { | |
if (!current) { | |
return; | |
} | |
if (!values[depth]) { | |
values[depth] = current.value; | |
} | |
else { | |
if (current.value > values[depth]) { | |
values[depth] = current.value; | |
} | |
} | |
traverse(current.left, depth + 1); | |
traverse(current.right, depth + 1); | |
} | |
traverse(current, depth); | |
return values; | |
} | |
return depthSearch(t); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment