150 najlepszych wywiadówŚredni

To samo drzewo binarne

Szczegółowy przewodnik i implementacja Python dla problemu „To samo drzewo binarne”.

Oświadczenie o problemie

Średni

Mając korzenie dwóch drzew binarnych p i q, napisz funkcję sprawdzającą, czy są one takie same, czy nie.

Dwa drzewa binarne są uważane za takie same, jeśli są strukturalnie identyczne, a węzły mają tę samą wartość.

Drzewa są reprezentowane jako listy o kolejności poziomów. Zaimplementuj funkcję isSameTree(p: list, q: list) -> bool.

Ograniczenia
  • The number of nodes in both trees is in the range [0, 100]
  • -10000 <= Node.val <= 10000

Przykłady

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

Both trees have the same structure and values.

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

In the first tree, 2 is the left child. In the second tree, 2 is the right child. Different structures.

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

Same structure but different values at the children.

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.