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
Throne Inheritance - DFS Tree Traversal INPUT: Family Tree king andy bob [X] catherine matthew alex asha Operations: 1. ThroneInheritance("king") 2. birth("king","andy") 3. birth("king","bob") 4. birth("king","catherine") 5. birth("andy","matthew") 6. birth("bob","alex") 7. birth("bob","asha") 8. getInheritanceOrder() 9. death("bob") 10. getInheritanceOrder() Legend: [X] = deceased ALGORITHM: DFS Traversal 1 Build Tree Structure Map parent --> children list king: [andy, bob, catherine] 2 Track Deaths Use Set to store dead people dead = {"bob"} 3 DFS Pre-order Traversal Visit parent before children def dfs(person): if person not dead: result.add(person) for child in children: dfs(child) 4 Skip Dead Members Dead people excluded from result but children included Order: king-->andy-->matthew-->bob... FINAL RESULT Before bob's death: ["king", "andy", "matthew", "bob", "alex", "asha", "catherine"] After bob's death: ["king", "andy", "matthew", "alex", "asha", "catherine"] DFS Order Visualization: 1 2 X 6 3 4 5 Key Insight: The inheritance follows a pre-order DFS traversal: visit parent first, then recursively visit children in birth order. Dead members are skipped in output but their subtrees are still traversed (children can still inherit). Time Complexity: O(N) for getInheritanceOrder where N = total family members. TutorialsPoint - Throne Inheritance | DFS Tree Traversal Approach
Asked in
Google 15 Amazon 12 Microsoft 8
31.5K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen