150 principais entrevistasDifícil

Profundidade Máxima da Árvore Binária

Guia detalhado e implementação de Python para o problema de 'Profundidade Máxima da Árvore Binária'.

Declaração do problema

Difícil

Dada a raiz de uma árvore binária, retorne sua profundidade máxima.

A profundidade máxima de uma árvore binária é o número de nós ao longo do caminho mais longo, do nó raiz até o nó folha mais distante.

A árvore é representada como uma lista de ordem de nível onde Nenhum representa um nó ausente. Implemente uma função maxDepth(root: list) -> int.

Restrições
  • The number of nodes in the tree is in the range [0, 10000]
  • -100 <= Node.val <= 100

Exemplos

Example 1
Input
[3,9,20,None,None,15,7]
Output
3
Explanation

The tree has 3 levels: root [3], second level [9,20], third level [15,7]. The longest path is 3->20->15 or 3->20->7, both of depth 3.

Example 2
Input
[1,None,2]
Output
2
Explanation

The tree has root 1 with only a right child 2. Depth is 2.

Example 3
Input
[]
Output
0
Explanation

An empty tree has depth 0.

Need a Hint?
Considere usar estruturas de dados específicas de árvores, como conjuntos ou heaps.
Edge Cases to Watch
  • Estruturas de entrada vazias
  • Entradas de elemento único
  • Grandes limites numéricos

Pronto para resolver?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Abrir no Editor
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

Recursos Python recomendados

Expanda seu conhecimento com tutoriais interativos relacionados, folhas de dicas e comparações de código.