Entrevista a los 150 mejoresMedio

Árbol binario equilibrado

Guía detallada e implementación de Python para el problema del 'Árbol binario equilibrado'.

Declaración del problema

Medio

Dado un árbol binario, determine si tiene equilibrio en altura.

Un árbol binario de altura equilibrada es un árbol binario en el que la profundidad de los dos subárboles de cada nodo nunca difiere en más de uno.

El árbol se representa como una lista de orden de niveles. Implementar una función isBalanced(root: list) -> bool.

Restricciones
  • The number of nodes in the tree is in the range [0, 5000]
  • -10000 <= Node.val <= 10000

Ejemplos

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

The left subtree (rooted at 9) has depth 1, and the right subtree (rooted at 20) has depth 2. The difference is 1, so the tree is balanced.

Example 2
Input
[1,2,2,3,3,None,None,4,4]
Output
False
Explanation

The left subtree has depth 3 while the right subtree has depth 1. The difference is 2, so the tree is not balanced.

Example 3
Input
[]
Output
True
Explanation

An empty tree is considered balanced.

Need a Hint?
Considere la posibilidad de utilizar estructuras de datos específicas de Trees, como conjuntos o montones.
Edge Cases to Watch
  • Estructuras de entrada vacías
  • Entradas de un solo elemento
  • Grandes límites numéricos

¿Listo para resolver?

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

Abrir en el 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 recomendados de Python

Amplíe sus conocimientos con tutoriales interactivos relacionados, hojas de trucos y comparaciones de códigos.