Sezione DSAMedio

Albero AVL

Guida dettagliata e implementazione Python per il problema 'AVL Tree'.

Dichiarazione del problema

Medio

Scrivi una funzione is_avl_balanced(tree_arr) che accetta una rappresentazione array di un albero binario tree_arr e restituisce True se l'albero è bilanciato in altezza (per ogni nodo, l'altezza dei suoi sottoalberi sinistro e destro differisce al massimo di 1) ed è un BST valido, o False altrimenti.

Vincoli
  • 0 <= len(tree_arr) <= 1000

Esempi

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

The tree is a valid BST and the depth difference of left/right subtrees of all nodes is at most 1.

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

The tree is unbalanced because leaf node 4 is at depth 4 while right subtree of node 1 is empty.

Need a Hint?
Prendi in considerazione l'utilizzo di strutture dati specifiche di Trees come set o heap.
Edge Cases to Watch
  • Strutture di input vuote
  • Ingressi a elemento singolo
  • Grandi limiti numerici

Pronto a risolvere?

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

Apri nell'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

Risorse Python consigliate

Espandi le tue conoscenze con tutorial interattivi, foglietti illustrativi e confronti di codici correlati.