Le migliori 150 intervisteMedio

Vista laterale destra dell'albero binario

Guida dettagliata e implementazione Python per il problema "Vista laterale destra dell'albero binario".

Dichiarazione del problema

Medio

Data la radice di un albero binario, immagina di stare sul lato destro di esso, restituisci i valori dei nodi che vedi ordinati dall'alto verso il basso.

L'albero è rappresentato come un elenco in ordine di livello. Implementa una funzione rightSideView(root: list) -> list.

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

Esempi

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

From the right side: at level 0 you see 1, at level 1 you see 3, at level 2 you see 4.

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

From the right side: at level 0 you see 1, at level 1 you see 3.

Example 3
Input
[]
Output
[]
Explanation

Empty tree, nothing to see.

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.