DSA 部分簡單

空白時間

“BST”問題的詳細指南和 Python 實作。

問題陳述

簡單

寫一個函數 is_valid_bst(tree_arr),它採用二元樹 tree_arr 的陣列表示(根位於索引 0,i 的子節點位於 2i+12i+2,其中 None 表示空節),如果它是有效節點的二元節點,否則傳回False

約束條件
  • 0 <= len(tree_arr) <= 1000

範例

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?
考慮使用樹特定的資料結構,例如集合或堆。
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 資源

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