DSA 部分中等

B樹

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

問題陳述

中等

寫一個函數 is_valid_btree_leaf_depth(keys, child_pointers, t) 來檢查 B 樹結構節點屬性是否有效。具體來說,如果所有葉節點都處於相同深度且每個節點(根除外)的鍵都在 t-12t-1 之間,則傳回 True,其中 t 是最小度數。輸入格式:keys 將節點 ID 對應到鍵列表,child_pointers 將節點 ID 對應到子 ID 列表,以及最小度數 t

約束條件
  • 2 <= t <= 10
  • 1 <= len(keys) <= 100

範例

Example 1
Input
keys = {1: [10, 20], 2: [5], 3: [15], 4: [25, 30]}, child_pointers = {1: [2, 3, 4]}, t = 2
Output
True
Explanation

Root 1 has keys [10, 20]. Children 2, 3, 4 are leaves at the same depth 1 and satisfy the key count constraint of 1 to 3 keys.

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 資源

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