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

通过相关的交互式教程、备忘单和代码比较来扩展您的知识。