Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ferdelamad/8513430ba676c89f2c6d3dc6b310556e to your computer and use it in GitHub Desktop.
Save ferdelamad/8513430ba676c89f2c6d3dc6b310556e to your computer and use it in GitHub Desktop.
Largest Tree
const findLargestLevel = function(n) {
//first create a holder var
const allNodes = [];
// define our recursive foo (node, depth)
function checkNodes (node, depth) {
// first add the value of the node to its current depth (in the array)
if (!allNodes[depth]) {
allNodes[depth] = 0;
}
allNodes[depth] += node.value
// if the node has children, loop over them and recursibly apply the foo
node.children.forEach(child => {
// increase the depth by 1
checkNodes(child, depth + 1);
})
}
checkNodes(n, 0);
return allNodes.indexOf(Math.max(...allNodes));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment