150強訪談中等

二元樹右側視圖

“二元樹右側視圖”問題的詳細指南和 Python 實作。

問題陳述

中等

給定二元樹的根,想像自己站在它的右側,返回您可以看到從上到下排序的節點的值。

該樹被表示為一個級別順序列表。實作函數 rightSideView(root: list) -> list

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

範例

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?
考慮使用樹特定的資料結構,例如集合或堆。
Edge Cases to Watch
  • 空輸入結構
  • 單元素輸入
  • 大數值範圍

準備好解決了嗎?

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

在編輯器中開啟
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

推薦的 Python 資源

透過相關的互動式教學、備忘單和程式碼比較來擴展您的知識。