Sezione DSAFacile

BST

Guida dettagliata e implementazione Python per il problema 'BST'.

Dichiarazione del problema

Facile

Scrivi una funzione is_valid_bst(tree_arr) che accetta una rappresentazione array di un albero binario tree_arr (radice all'indice 0, figli di i a 2i+1 e 2i+2, con None che rappresenta i nodi vuoti) e restituisce True se è un albero di ricerca binario (BST) valido, oppure False altrimenti.

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

Esempi

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

The left child 1 is smaller than root 2, and right child 3 is greater than root 2.

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

The root value is 5, but its right child 4 contains a left child 3 which is smaller than 5.

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.