Skip to content

Practice Throne Inheritance

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#1600
LeetCode Problem

Throne Inheritance

Problem Statement

ThroneInheritance(kingName) starts a family. birth(parentName,childName) appends a child in birth order; death(name) marks a person dead. getInheritanceOrder() lists living people in preorder: parent, then each child's full descendants from oldest child to youngest. A dead person is omitted but their descendants retain their place. Practice approach: Store ordered child lists and a dead-name set. Traverse the complete family tree, skipping only dead names in output, never their subtrees. An iterative traversal avoids stack overflow for a long lineage. Birth and death are expected O(1); listing is O(number of people).

Source: Throne Inheritance on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
["ThroneInheritance","birth","birth","birth","death","getInheritanceOrder"]
[["rex"],["rex","ada"],["rex","ben"],["ada","cy"],["ada"],[]]
Output
[null,null,null,null,null,["rex","cy","ben"]]
Explanation

Ada is omitted after death, but her child Cy still precedes Ada's younger sibling Ben.

Constraints

Names are lowercase and 1 to 15 letters.
King and child names are distinct; named people already exist for death.
A birth parent is alive.
At most 10^5 birth/death calls and 10 order queries.

Related LeetCode Problems

Practice more problems similar to Throne Inheritance.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.