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.
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
["ThroneInheritance","birth","birth","birth","death","getInheritanceOrder"] [["rex"],["rex","ada"],["rex","ben"],["ada","cy"],["ada"],[]]
[null,null,null,null,null,["rex","cy","ben"]]
Ada is omitted after death, but her child Cy still precedes Ada's younger sibling Ben.
Constraints
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.