|
| 1 | +# [1600. Throne Inheritance](https://leetcode.com/problems/throne-inheritance/) |
| 2 | + |
| 3 | + |
| 4 | +## 题目 |
| 5 | + |
| 6 | +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. |
| 7 | + |
| 8 | +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. |
| 9 | + |
| 10 | +``` |
| 11 | +Successor(x, curOrder): |
| 12 | + if x has no children or all of x's children are in curOrder: |
| 13 | + if x is the king return null |
| 14 | + else return Successor(x's parent, curOrder) |
| 15 | + else return x's oldest child who's not in curOrder |
| 16 | +``` |
| 17 | + |
| 18 | +For example, assume we have a kingdom that consists of the king, his children Alice and Bob (Alice is older than Bob), and finally Alice's son Jack. |
| 19 | + |
| 20 | +1. In the beginning, `curOrder` will be `["king"]`. |
| 21 | +2. Calling `Successor(king, curOrder)` will return Alice, so we append to `curOrder` to get `["king", "Alice"]`. |
| 22 | +3. Calling `Successor(Alice, curOrder)` will return Jack, so we append to `curOrder` to get `["king", "Alice", "Jack"]`. |
| 23 | +4. Calling `Successor(Jack, curOrder)` will return Bob, so we append to `curOrder` to get `["king", "Alice", "Jack", "Bob"]`. |
| 24 | +5. Calling `Successor(Bob, curOrder)` will return `null`. Thus the order of inheritance will be `["king", "Alice", "Jack", "Bob"]`. |
| 25 | + |
| 26 | +Using the above function, we can always obtain a unique order of inheritance. |
| 27 | + |
| 28 | +Implement the `ThroneInheritance` class: |
| 29 | + |
| 30 | +- `ThroneInheritance(string kingName)` Initializes an object of the `ThroneInheritance` class. The name of the king is given as part of the constructor. |
| 31 | +- `void birth(string parentName, string childName)` Indicates that `parentName` gave birth to `childName`. |
| 32 | +- `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. |
| 33 | +- `string[] getInheritanceOrder()` Returns a list representing the current order of inheritance **excluding** dead people. |
| 34 | + |
| 35 | +**Example 1:** |
| 36 | + |
| 37 | +``` |
| 38 | +Input |
| 39 | +["ThroneInheritance", "birth", "birth", "birth", "birth", "birth", "birth", "getInheritanceOrder", "death", "getInheritanceOrder"] |
| 40 | +[["king"], ["king", "andy"], ["king", "bob"], ["king", "catherine"], ["andy", "matthew"], ["bob", "alex"], ["bob", "asha"], [null], ["bob"], [null]] |
| 41 | +Output |
| 42 | +[null, null, null, null, null, null, null, ["king", "andy", "matthew", "bob", "alex", "asha", "catherine"], null, ["king", "andy", "matthew", "alex", "asha", "catherine"]] |
| 43 | +
|
| 44 | +Explanation |
| 45 | +ThroneInheritance t= new ThroneInheritance("king"); // order:king |
| 46 | +t.birth("king", "andy"); // order: king >andy |
| 47 | +t.birth("king", "bob"); // order: king > andy >bob |
| 48 | +t.birth("king", "catherine"); // order: king > andy > bob >catherine |
| 49 | +t.birth("andy", "matthew"); // order: king > andy >matthew > bob > catherine |
| 50 | +t.birth("bob", "alex"); // order: king > andy > matthew > bob >alex > catherine |
| 51 | +t.birth("bob", "asha"); // order: king > andy > matthew > bob > alex >asha > catherine |
| 52 | +t.getInheritanceOrder(); // return ["king", "andy", "matthew", "bob", "alex", "asha", "catherine"] |
| 53 | +t.death("bob"); // order: king > andy > matthew >bob > alex > asha > catherine |
| 54 | +t.getInheritanceOrder(); // return ["king", "andy", "matthew", "alex", "asha", "catherine"] |
| 55 | +
|
| 56 | +``` |
| 57 | + |
| 58 | +**Constraints:** |
| 59 | + |
| 60 | +- `1 <= kingName.length, parentName.length, childName.length, name.length <= 15` |
| 61 | +- `kingName`, `parentName`, `childName`, and `name` consist of lowercase English letters only. |
| 62 | +- All arguments `childName` and `kingName` are **distinct**. |
| 63 | +- All `name` arguments of `death` will be passed to either the constructor or as `childName` to `birth` first. |
| 64 | +- For each call to `birth(parentName, childName)`, it is guaranteed that `parentName` is alive. |
| 65 | +- At most `105` calls will be made to `birth` and `death`. |
| 66 | +- At most `10` calls will be made to `getInheritanceOrder`. |
| 67 | + |
| 68 | +## 题目大意 |
| 69 | + |
| 70 | +一个王国里住着国王、他的孩子们、他的孙子们等等。每一个时间点,这个家庭里有人出生也有人死亡。这个王国有一个明确规定的皇位继承顺序,第一继承人总是国王自己。我们定义递归函数 Successor(x, curOrder) ,给定一个人 x 和当前的继承顺序,该函数返回 x 的下一继承人。 |
| 71 | + |
| 72 | +## 解题思路 |
| 73 | + |
| 74 | +- 这道题思路不难。先将国王每个孩子按照顺序存在一个 map 中,然后每个国王的孩子还存在父子关系,同理也按顺序存在 map 中。执行 GetInheritanceOrder() 函数时,将国王的孩子按顺序遍历,如果每个孩子还有孩子,递归遍历到底。如果把继承关系看成一棵树,此题便是多叉树的先根遍历的问题。 |
| 75 | + |
| 76 | +## 代码 |
| 77 | + |
| 78 | +```go |
| 79 | +package leetcode |
| 80 | + |
| 81 | +type ThroneInheritance struct { |
| 82 | + king string |
| 83 | + edges map[string][]string |
| 84 | + dead map[string]bool |
| 85 | +} |
| 86 | + |
| 87 | +func Constructor(kingName string) (t ThroneInheritance) { |
| 88 | + return ThroneInheritance{kingName, map[string][]string{}, map[string]bool{}} |
| 89 | +} |
| 90 | + |
| 91 | +func (t *ThroneInheritance) Birth(parentName, childName string) { |
| 92 | + t.edges[parentName] = append(t.edges[parentName], childName) |
| 93 | +} |
| 94 | + |
| 95 | +func (t *ThroneInheritance) Death(name string) { |
| 96 | + t.dead[name] = true |
| 97 | +} |
| 98 | + |
| 99 | +func (t *ThroneInheritance) GetInheritanceOrder() (res []string) { |
| 100 | + var preorder func(string) |
| 101 | + preorder = func(name string) { |
| 102 | + if !t.dead[name] { |
| 103 | + res = append(res, name) |
| 104 | + } |
| 105 | + for _, childName := range t.edges[name] { |
| 106 | + preorder(childName) |
| 107 | + } |
| 108 | + } |
| 109 | + preorder(t.king) |
| 110 | + return |
| 111 | +} |
| 112 | + |
| 113 | +/** |
| 114 | + * Your ThroneInheritance object will be instantiated and called as such: |
| 115 | + * obj := Constructor(kingName); |
| 116 | + * obj.Birth(parentName,childName); |
| 117 | + * obj.Death(name); |
| 118 | + * param_3 := obj.GetInheritanceOrder(); |
| 119 | + */ |
| 120 | +``` |
| 121 | + |
| 122 | + |
| 123 | +---------------------------------------------- |
| 124 | +<div style="display: flex;justify-content: space-between;align-items: center;"> |
| 125 | +<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1500~1599/1579.Remove-Max-Number-of-Edges-to-Keep-Graph-Fully-Traversable/">⬅️上一页</a></p> |
| 126 | +<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1600~1699/1603.Design-Parking-System/">下一页➡️</a></p> |
| 127 | +</div> |
0 commit comments