Throne Inheritance - Problem
A kingdom consists of a king, his children, his grandchildren, and so on. Every once in a while, someone in the family dies or a child is born.
The kingdom has a well-defined order of inheritance that consists of the king as the first member. Let's define the recursive function Successor(x, curOrder), which given a person x and the inheritance order so far, returns who should be the next person after x in the order of inheritance.
Successor(x, curOrder):
- if x has no children or all of x's children are in curOrder: if x is the king return null, else return Successor(x's parent, curOrder)
- else return x's oldest child who's not in curOrder
Implement the ThroneInheritance class:
ThroneInheritance(string kingName)Initializes an object of the ThroneInheritance class. The name of the king is given as part of the constructor.void birth(string parentName, string childName)Indicates that parentName gave birth to childName.void death(string name)Indicates the death of name. The death of the person doesn't affect the Successor function nor the current inheritance order. You can treat it as just marking the person as dead.string[] getInheritanceOrder()Returns a list representing the current order of inheritance excluding dead people.
Input & Output
Example 1 — Basic Operations
$
Input:
operations = [["ThroneInheritance", "king"], ["birth", "king", "andy"], ["birth", "king", "bob"], ["birth", "king", "catherine"], ["birth", "andy", "matthew"], ["birth", "bob", "alex"], ["birth", "bob", "asha"], ["getInheritanceOrder"], ["death", "bob"], ["getInheritanceOrder"]]
›
Output:
[null, null, null, null, null, null, null, ["king", "andy", "matthew", "bob", "alex", "asha", "catherine"], null, ["king", "andy", "matthew", "alex", "asha", "catherine"]]
💡 Note:
Initially: king -> andy (matthew) -> bob (alex, asha) -> catherine. After bob dies, inheritance skips bob but includes his children.
Example 2 — King Dies
$
Input:
operations = [["ThroneInheritance", "king"], ["birth", "king", "andy"], ["getInheritanceOrder"], ["death", "king"], ["getInheritanceOrder"]]
›
Output:
[null, null, ["king", "andy"], null, ["andy"]]
💡 Note:
When king dies, andy becomes the first in inheritance order
Example 3 — Deep Family Tree
$
Input:
operations = [["ThroneInheritance", "king"], ["birth", "king", "andy"], ["birth", "andy", "jack"], ["birth", "jack", "noah"], ["getInheritanceOrder"]]
›
Output:
[null, null, null, null, ["king", "andy", "jack", "noah"]]
💡 Note:
Deep tree follows DFS: king -> andy -> jack -> noah
Constraints
- 1 ≤ kingName.length, parentName.length, childName.length, name.length ≤ 15
- kingName, parentName, childName, and name consist of lowercase English letters only.
- All arguments kingName, parentName, childName, and name are distinct.
- All name arguments of death will be passed to either ThroneInheritance constructor or as childName to birth first.
- For each call to birth(parentName, childName), it is guaranteed that parentName is alive.
- At most 105 calls will be made to birth and death.
- At most 10 calls will be made to getInheritanceOrder.
Visualization
Tap to expand
Understanding the Visualization
1
Family Tree
Build tree structure with parent-child relationships
2
Track Deaths
Mark deceased members without removing from tree
3
DFS Inheritance
Traverse tree depth-first, skip dead people
Key Takeaway
🎯 Key Insight: The inheritance order is simply a DFS traversal of the family tree, skipping deceased members
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code