Solución
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