Maximum Depth of a Binary Tree

Write the code, see it run, pass the checks.

The problem

  • easy
  • Code

Write maxDepth(root). A binary tree is made of nodes { value, left, right }, where left and right are child nodes or null; root is the top node, or null for an empty tree. Return the tree's depth: the number of nodes on the longest path from the root down to a leaf. An empty tree has depth 0 and a single node depth 1. Don't change the tree. For example, the tree written in level order as [3, 9, 20, null, null, 15, 7], with null for a missing child, has 3 at the root, 9 and 20 below it, and 15 and 7 under 20, so its depth is 3. The checks include a tree 5,000 levels deep.

Your code must pass

  • Measures a balanced tree
  • Measures an uneven tree
  • Returns 0 for an empty tree and 1 for a single node
  • Measures a tree 5,000 levels deep