Throne Inheritance - Problem
๐ Throne Inheritance System
Imagine you're designing a digital system for a medieval kingdom to track the line of succession to the throne. The kingdom follows a specific inheritance order based on a depth-first traversal of the royal family tree.
How it works:
- The king is always first in line
- Children inherit before their aunts/uncles (depth-first)
- Among siblings, older children inherit before younger ones
- When someone dies, they're marked as deceased but stay in the family tree
Your task: Implement a ThroneInheritance class that can:
birth(parent, child)- Add a new child to the familydeath(person)- Mark someone as deceasedgetInheritanceOrder()- Return current inheritance order (excluding dead people)
Example: King โ Alice โ Alice's son Jack โ Bob
If Alice dies, the order becomes: King โ Jack โ Bob
Input & Output
basic_inheritance.py โ Python
$
Input:
ThroneInheritance("king")
birth("king", "andy")
birth("king", "bob")
birth("king", "catherine")
birth("andy", "matthew")
birth("bob", "alex")
birth("bob", "asha")
getInheritanceOrder()
โบ
Output:
["king", "andy", "matthew", "bob", "alex", "asha", "catherine"]
๐ก Note:
DFS traversal: King โ Andy โ Andy's children (Matthew) โ Bob โ Bob's children (Alex, Asha) โ Catherine
with_deaths.py โ Python
$
Input:
ThroneInheritance("king")
birth("king", "andy")
birth("king", "bob")
birth("andy", "matthew")
death("andy")
getInheritanceOrder()
โบ
Output:
["king", "matthew", "bob"]
๐ก Note:
Andy dies but stays in family tree. Matthew (Andy's son) inherits Andy's position. Order: King โ Matthew โ Bob
complex_family.py โ Python
$
Input:
ThroneInheritance("king")
birth("king", "alice")
birth("king", "bob")
birth("alice", "jack")
birth("alice", "jill")
birth("bob", "charlie")
death("alice")
death("bob")
getInheritanceOrder()
โบ
Output:
["king", "jack", "jill", "charlie"]
๐ก Note:
Both Alice and Bob die, but their children inherit their positions in the family tree structure
Visualization
Tap to expand
Understanding the Visualization
1
Build Family Tree
Each birth adds a child to their parent's children list
2
Track Deaths
Dead people stay in tree but are marked in a separate set
3
DFS Traversal
Visit king first, then each child's subtree completely before siblings
Key Takeaway
๐ฏ Key Insight: Throne inheritance is just a DFS traversal of the family tree, where we maintain parent-child relationships efficiently using a hash map and skip deceased members during traversal.
Time & Space Complexity
Time Complexity
O(n)
Each getInheritanceOrder takes O(n) time to visit each person once via DFS
โ Linear Growth
Space Complexity
O(n)
Space for children hash map and dead set, both proportional to number of people
โก Linearithmic Space
Constraints
- 1 โค kingName.length, parentName.length, childName.length, name.length โค 15
- kingName, parentName, childName, and name consist of lowercase English letters only
- All arguments childName and kingName are distinct
- All name arguments of death will be passed to either the constructor or as childName to birth first
- At most 105 calls will be made to birth and death
- At most 104 calls will be made to getInheritanceOrder
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code