Sezione DSAMedio

Albero pieghevole

Guida dettagliata e implementazione Python per il problema "Albero pieghevole".

Dichiarazione del problema

Medio

Scrivi una funzione is_foldable(tree_arr) che accetta una rappresentazione array di un albero binario tree_arr e controlla se l'albero è pieghevole. Un albero è pieghevole se i suoi sottoalberi sinistro e destro sono specchi strutturali l'uno dell'altro (il valore non conta, solo la struttura).

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

Esempi

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

Left child has right child 4. Right child has left child 5. Structures are symmetric mirrors.

Example 2
Input
tree_arr = [1, 2, 3, 4, None, 5, None]
Output
False
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.