← 返回 snowflake 的题目列表Throne Inheritance
类型:qbank
A kingdom consists of a king and his children in a family tree.
Problem Overview: Throne Inheritance
We need to create a system to manage a family tree for a kingdom. This kingdom starts with a king, and the tree grows as children are born. Every person in this kingdom has a unique name.
The system is initialized with a king named kingName. You must build a class that supports the following actions:
ThroneInheritance(string kingName)
Starts the system and sets the king as the root of the family tree.
void birth(string parentName, string childName)
Records that a parent named parentName had a new child named childName.
void death(string name)
Marks the person named name as dead.
string[] getInheritanceOrder()
Returns a list of names showing who is next in line for the throne.
The order of inheritance is determined by a standard Preorder Traversal of the family tree. The rules are:
The parent comes before their children.
Older children come before younger children.
If a person has died, they are skipped and do not appear in the final list.
Sample Usage
Example 1:
Input: ["ThroneInheritance","birth","birth","birth","birth","birth","birth","getInheritanceOrder","death","getInheritanceOrder"] [["king"],["king","andy"],["king","bob"],["king","catherine"],["andy","matthew"],["bob","alex"],["bob","asha"],[],["bob"],[]]
Output: [null,null,null,null,null,null,null,["king","andy","matthew","bob","alex","asha","catherine"],null,["king","andy","matthew","alex","asha","catherine"]]
Technical Limits
Name Length: kingName, parentName, childName, and name are between 1 and 15 characters long.
Characters: Names use only lowercase English letters.
Uniqueness: Every childName is unique.
Valid Parents: When birth is called, the parentName is guaranteed to exist in the tree.
Valid Deaths: The death function is only called for people who are currently alive.
Operation Count: There will be at most 10^5 total calls to birth, death, and getInheritanceOrder.