Sekcja DSAŁatwe

BST

Szczegółowy przewodnik i implementacja Python dla problemu „BST”.

Oświadczenie o problemie

Łatwe

Napisz funkcję is_valid_bst(tree_arr), która przyjmuje tablicową reprezentację drzewa binarnego tree_arr (korzeń w indeksie 0, dzieci i w 2i+1 i 2i+2, gdzie None reprezentuje puste węzły) i zwraca True, jeśli jest to prawidłowe drzewo wyszukiwania binarnego (BST), lub False w przeciwnym razie.

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

Przykłady

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?
Rozważ użycie struktur danych specyficznych dla drzew, takich jak zestawy lub sterty.
Edge Cases to Watch
  • Puste struktury wejściowe
  • Wejścia jednoelementowe
  • Duże granice liczbowe

Gotowy do rozwiązania?

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

Otwórz w Edytorze
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

Polecane zasoby Pythona

Poszerzaj swoją wiedzę dzięki powiązanym interaktywnym samouczkom, ściągawkom i porównaniom kodów.