LeetCode 题解工作台
王位继承顺序
一个王国里住着国王、他的孩子们、他的孙子们等等。每一个时间点,这个家庭里有人出生也有人死亡。 这个王国有一个明确规定的王位继承顺序,第一继承人总是国王自己。我们定义递归函数 Successor(x, curOrder) ,给定一个人 x 和当前的继承顺序,该函数返回 x 的下一继承人。 Succes…
4
题型
6
代码语言
3
相关题
当前训练重点
中等 · 二分·树·traversal
答案摘要
根据题目描述,我们可以发现,王位继承顺序实际上是一个多叉树的前序遍历。我们可以使用一个哈希表 存储每个人的孩子,使用一个集合 存储已经去世的人。 - 调用 `birth(parentName, childName)` 时,我们将 `childName` 添加到 `parentName` 的孩子列表中。
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 二分·树·traversal 题型思路
题目描述
一个王国里住着国王、他的孩子们、他的孙子们等等。每一个时间点,这个家庭里有人出生也有人死亡。
这个王国有一个明确规定的王位继承顺序,第一继承人总是国王自己。我们定义递归函数 Successor(x, curOrder) ,给定一个人 x 和当前的继承顺序,该函数返回 x 的下一继承人。
Successor(x, curOrder):
如果 x 没有孩子或者所有 x 的孩子都在 curOrder 中:
如果 x 是国王,那么返回 null
否则,返回 Successor(x 的父亲, curOrder)
否则,返回 x 不在 curOrder 中最年长的孩子
比方说,假设王国由国王,他的孩子 Alice 和 Bob (Alice 比 Bob 年长)和 Alice 的孩子 Jack 组成。
- 一开始,
curOrder为["king"]. - 调用
Successor(king, curOrder),返回 Alice ,所以我们将 Alice 放入curOrder中,得到["king", "Alice"]。 - 调用
Successor(Alice, curOrder),返回 Jack ,所以我们将 Jack 放入curOrder中,得到["king", "Alice", "Jack"]。 - 调用
Successor(Jack, curOrder),返回 Bob ,所以我们将 Bob 放入curOrder中,得到["king", "Alice", "Jack", "Bob"]。 - 调用
Successor(Bob, curOrder),返回null。最终得到继承顺序为["king", "Alice", "Jack", "Bob"]。
通过以上的函数,我们总是能得到一个唯一的继承顺序。
请你实现 ThroneInheritance 类:
ThroneInheritance(string kingName)初始化一个ThroneInheritance类的对象。国王的名字作为构造函数的参数传入。void birth(string parentName, string childName)表示parentName新拥有了一个名为childName的孩子。void death(string name)表示名为name的人死亡。一个人的死亡不会影响Successor函数,也不会影响当前的继承顺序。你可以只将这个人标记为死亡状态。string[] getInheritanceOrder()返回 除去 死亡人员的当前继承顺序列表。
示例:
输入:
["ThroneInheritance", "birth", "birth", "birth", "birth", "birth", "birth", "getInheritanceOrder", "death", "getInheritanceOrder"]
[["king"], ["king", "andy"], ["king", "bob"], ["king", "catherine"], ["andy", "matthew"], ["bob", "alex"], ["bob", "asha"], [null], ["bob"], [null]]
输出:
[null, null, null, null, null, null, null, ["king", "andy", "matthew", "bob", "alex", "asha", "catherine"], null, ["king", "andy", "matthew", "alex", "asha", "catherine"]]
解释:
ThroneInheritance t= new ThroneInheritance("king"); // 继承顺序:king
t.birth("king", "andy"); // 继承顺序:king > andy
t.birth("king", "bob"); // 继承顺序:king > andy > bob
t.birth("king", "catherine"); // 继承顺序:king > andy > bob > catherine
t.birth("andy", "matthew"); // 继承顺序:king > andy > matthew > bob > catherine
t.birth("bob", "alex"); // 继承顺序:king > andy > matthew > bob > alex > catherine
t.birth("bob", "asha"); // 继承顺序:king > andy > matthew > bob > alex > asha > catherine
t.getInheritanceOrder(); // 返回 ["king", "andy", "matthew", "bob", "alex", "asha", "catherine"]
t.death("bob"); // 继承顺序:king > andy > matthew > bob(已经去世)> alex > asha > catherine
t.getInheritanceOrder(); // 返回 ["king", "andy", "matthew", "alex", "asha", "catherine"]
提示:
1 <= kingName.length, parentName.length, childName.length, name.length <= 15kingName,parentName,childName和name仅包含小写英文字母。- 所有的参数
childName和kingName互不相同。 - 所有
death函数中的死亡名字name要么是国王,要么是已经出生了的人员名字。 - 每次调用
birth(parentName, childName)时,测试用例都保证parentName对应的人员是活着的。 - 最多调用
105次birth和death。 - 最多调用
10次getInheritanceOrder。
解题思路
方法一:多叉树的前序遍历
根据题目描述,我们可以发现,王位继承顺序实际上是一个多叉树的前序遍历。我们可以使用一个哈希表 存储每个人的孩子,使用一个集合 存储已经去世的人。
- 调用
birth(parentName, childName)时,我们将childName添加到parentName的孩子列表中。 - 调用
death(name)时,我们将name添加到dead集合中。 - 调用
getInheritanceOrder()时,我们从国王开始进行深度优先搜索,如果当前节点x没有去世,我们将x添加到答案列表中,然后递归地遍历x的所有孩子。
时间复杂度方面,birth 和 death 的时间复杂度均为 ,getInheritanceOrder 的时间复杂度为 ,空间复杂度为 。其中 是节点数量。
class ThroneInheritance:
def __init__(self, kingName: str):
self.king = kingName
self.dead = set()
self.g = defaultdict(list)
def birth(self, parentName: str, childName: str) -> None:
self.g[parentName].append(childName)
def death(self, name: str) -> None:
self.dead.add(name)
def getInheritanceOrder(self) -> List[str]:
def dfs(x: str):
x not in self.dead and ans.append(x)
for y in self.g[x]:
dfs(y)
ans = []
dfs(self.king)
return ans
# Your ThroneInheritance object will be instantiated and called as such:
# obj = ThroneInheritance(kingName)
# obj.birth(parentName,childName)
# obj.death(name)
# param_3 = obj.getInheritanceOrder()
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | complexity for getInheritanceOrder is O(N) where N is the total number of living descendants, as DFS visits each node once. Birth and death operations are O(1) due to hash map access and set marking. Space complexity is O(N) for storing the tree nodes and live/deceased states. |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Clarify whether deaths remove nodes or just mark them.
- question_mark
Expect a tree structure that preserves children order for DFS traversal.
- question_mark
Ask about efficient retrieval after multiple births and deaths without full recomputation.
常见陷阱
外企场景- error
Forgetting to skip deceased members in DFS leading to incorrect inheritance order.
- error
Modifying the tree structure on death instead of using a marker, which complicates updates.
- error
Assuming unordered children or using BFS, which breaks the oldest-to-youngest traversal requirement.
进阶变体
外企场景- arrow_right_alt
Track inheritance with arbitrary parent-to-child ordering instead of oldest-first priority.
- arrow_right_alt
Allow multiple kings or parallel family branches requiring merged DFS traversal.
- arrow_right_alt
Implement additional queries like 'who is the nth in line' without traversing the full tree.