150 najlepszych wywiadówŚredni

Widok z prawej strony drzewa binarnego

Szczegółowy przewodnik i implementacja Python dla problemu „Widok z prawej strony drzewa binarnego”.

Oświadczenie o problemie

Średni

Biorąc pod uwagę korzeń drzewa binarnego, wyobraź sobie, że stoisz po jego prawej stronie, zwróć wartości węzłów, które widzisz, uporządkowane od góry do dołu.

Drzewo jest reprezentowane jako lista poziomów. Zaimplementuj funkcję rightSideView(root: list) -> list.

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

Przykłady

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?
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.