tree: 7 nodesexpected 3

Loading 3D view…

A tree with 7 nodes

step 1 / 111

Solution

TypeScript · line 1
function maxDepth(tree: Tree): number {
  function depth(id: string | null): number {
    if (id === null) {
      return 0;
    }
    const [left, right] = tree.nodes[id].children;
    const l = depth(left);
    const r = depth(right);
    return 1 + Math.max(l, r);
  }
  return depth(tree.root);
}