Created
July 5, 2018 16:57
-
-
Save ferdelamad/8513430ba676c89f2c6d3dc6b310556e to your computer and use it in GitHub Desktop.
Largest Tree
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
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