Skip to content

Instantly share code, notes, and snippets.

@hamannjames
Created August 4, 2017 03:50
Show Gist options
  • Save hamannjames/4e891ba16bff12404dbcf99378122913 to your computer and use it in GitHub Desktop.
Save hamannjames/4e891ba16bff12404dbcf99378122913 to your computer and use it in GitHub Desktop.
Get largest value in tree depth using depth first search
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