How deep does this tree go?
Nested comment threads, category menus and file explorers all arrive as trees, and the first question is usually how deep they go: how far to indent, when to collapse, whether to warn that a reply is nested too far.
Implement maxDepth(node). A node is a plain object shaped { id, children } where children is an array of nodes; the children property may be missing entirely, which means the node has no children. Return the number of levels in the tree: 0 for null, 1 for a node with no children, and one more than the deepest child otherwise.
What it has to do
- Return 0 when the node is
null. - Return 1 for a leaf node, whether
childrenis missing or an empty array. - Follow the deepest branch, not the first one.
- Handle trees of any depth.
Your workspace
Ready to check it?
5 tests run against your code, right here in your browser. Sign in to claim the XP when you pass.
AI Crack & Solution Assist
Stuck? Get instant AI hints or break down the optimal solution.
Stuck? The javascript course covers everything this challenge needs.