Solución

@jonathanlnb·4/5/2026TypeScript
solution.tsTypeScript
interface TreeNode {
  value: number;
  left: TreeNode | null;
  right: TreeNode | null;
}

function maxDepth(root: TreeNode | null): number {
  if(root === null) return 0;

  return searchDepth(root, 0);
}

function searchDepth(root: TreeNode | null, actualDepth: number) : number {
  let finalDepth = actualDepth +1;
  let leftDepth = 0;
  let rightDepth = 0;
  if(root.left === null && root.right === null) return finalDepth;
  if(root.left !== null) leftDepth = searchDepth(root.left, actualDepth+1);
  if(root.right !== null) rightDepth = searchDepth(root.right, actualDepth+1);
  finalDepth = Math.max(leftDepth, rightDepth);
  return finalDepth;
}

// No modificar: necesario para evaluar el resultado.
export { maxDepth };
0respuestas
Respuestas

Aún no hay respuestas

¡Sé el primero en responder!

Escribir un comentario

Recuerda ser amable. Estás comentando la solución de otra persona. Comparte tu perspectiva de forma constructiva y respetuosa.

Debes iniciar sesión para publicar un comentario.