Sezione DSAMedio

Albero simmetrico

Guida dettagliata e implementazione Python per il problema 'Albero simmetrico'.

Dichiarazione del problema

Medio

Scrivi una funzione is_symmetric(tree_arr) che accetta una rappresentazione array di un albero binario tree_arr e controlla se l'albero è simmetrico (uno specchio di se stesso attorno al centro, che corrisponde sia alla struttura che ai valori).

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

Esempi

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

The tree is symmetric in both shape and node values.

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

Left child 2 has right child 3, right child 2 has right child 3. Values match but structures are not mirrors.

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.