@@ -83,20 +83,23 @@ t.getInheritanceOrder(); // 返回 ["king", "andy", "matthew", "alex", "asha", "
83
83
84
84
### 方法一:多叉树的前序遍历
85
85
86
- 可以发现,题目中王位的继承顺序,实际上是多叉树的前序遍历 。
86
+ 根据题目描述,我们可以发现,王位继承顺序实际上是一个多叉树的前序遍历。我们可以使用一个哈希表 $g$ 存储每个人的孩子,使用一个集合 $dead$ 存储已经去世的人 。
87
87
88
- 我们采用哈希表建树,得到 ` g ` ,用哈希表 ` dead ` 保存死亡人员。获取继承顺序时,采用先序遍历的方式,把活着的人员放入结果数组中。
88
+ - 调用 ` birth(parentName, childName) ` 时,我们将 ` childName ` 添加到 ` parentName ` 的孩子列表中。
89
+ - 调用 ` death(name) ` 时,我们将 ` name ` 添加到 ` dead ` 集合中。
90
+ - 调用 ` getInheritanceOrder() ` 时,我们从国王开始进行深度优先搜索,如果当前节点 ` x ` 没有去世,我们将 ` x ` 添加到答案列表中,然后递归地遍历 ` x ` 的所有孩子。
89
91
90
- 获取继承顺序的时间复杂度是 $O(n )$,其他操作的时间复杂度是 $O(1 )$,空间复杂度 $O(n)$。其中 $n$ 是树中的节点数 。
92
+ 时间复杂度方面, ` birth ` 和 ` death ` 的时间复杂度均为 $O(1 )$,` getInheritanceOrder ` 的时间复杂度为 $O(n )$,空间复杂度为 $O(n)$。其中 $n$ 是节点数量 。
91
93
92
94
<!-- tabs:start -->
93
95
94
96
``` python
95
97
class ThroneInheritance :
98
+
96
99
def __init__ (self , kingName : str ):
97
- self .g = defaultdict(list )
98
- self .dead = set ()
99
100
self .king = kingName
101
+ self .dead = set ()
102
+ self .g = defaultdict(list )
100
103
101
104
def birth (self , parentName : str , childName : str ) -> None :
102
105
self .g[parentName].append(childName)
@@ -105,9 +108,8 @@ class ThroneInheritance:
105
108
self .dead.add(name)
106
109
107
110
def getInheritanceOrder (self ) -> List[str ]:
108
- def dfs (x ):
109
- if x not in self .dead:
110
- ans.append(x)
111
+ def dfs (x : str ):
112
+ x not in self .dead and ans.append(x)
111
113
for y in self .g[x]:
112
114
dfs(y)
113
115
@@ -125,10 +127,10 @@ class ThroneInheritance:
125
127
126
128
``` java
127
129
class ThroneInheritance {
128
- private Map<String , List<String > > g = new HashMap<> ();
129
- private Set<String > dead = new HashSet<> ();
130
- private List<String > ans;
131
130
private String king;
131
+ private Set<String > dead = new HashSet<> ();
132
+ private Map<String , List<String > > g = new HashMap<> ();
133
+ private List<String > ans = new ArrayList<> ();
132
134
133
135
public ThroneInheritance (String kingName ) {
134
136
king = kingName;
@@ -143,7 +145,7 @@ class ThroneInheritance {
143
145
}
144
146
145
147
public List<String > getInheritanceOrder () {
146
- ans = new ArrayList<> ();
148
+ ans. clear ();
147
149
dfs(king);
148
150
return ans;
149
151
}
@@ -152,7 +154,7 @@ class ThroneInheritance {
152
154
if (! dead. contains(x)) {
153
155
ans. add(x);
154
156
}
155
- for (String y : g. getOrDefault(x, Collections . emptyList ())) {
157
+ for (String y : g. getOrDefault(x, List . of ())) {
156
158
dfs(y);
157
159
}
158
160
}
@@ -170,17 +172,12 @@ class ThroneInheritance {
170
172
``` cpp
171
173
class ThroneInheritance {
172
174
public:
173
- unordered_map<string, vector<string >> g;
174
- unordered_set<string > dead;
175
- string king;
176
- vector<string > ans;
177
-
178
175
ThroneInheritance(string kingName) {
179
176
king = kingName;
180
177
}
181
178
182
179
void birth(string parentName, string childName) {
183
- g[ parentName] .push_back (childName);
180
+ g[parentName].emplace_back (childName);
184
181
}
185
182
186
183
void death (string name) {
@@ -193,9 +190,15 @@ public:
193
190
return ans;
194
191
}
195
192
193
+ private:
194
+ string king;
195
+ unordered_set<string > dead;
196
+ unordered_map<string, vector<string >> g;
197
+ vector<string > ans;
198
+
196
199
void dfs(string& x) {
197
- if (!dead.count (x)) {
198
- ans.push_back (x);
200
+ if (!dead.contains (x)) {
201
+ ans.emplace_back (x);
199
202
}
200
203
for (auto& y : g[x]) {
201
204
dfs(y);
@@ -214,29 +217,29 @@ public:
214
217
215
218
```go
216
219
type ThroneInheritance struct {
217
- g map[string][]string
218
- dead map[string]bool
219
220
king string
221
+ dead map[string]bool
222
+ g map[string][]string
220
223
}
221
224
225
+
222
226
func Constructor(kingName string) ThroneInheritance {
223
- g := map[string][]string{}
224
- dead := map[string]bool{}
225
- return ThroneInheritance{g, dead, kingName}
227
+ return ThroneInheritance{kingName, map[string]bool{}, map[string][]string{}}
226
228
}
227
229
228
- func (this *ThroneInheritance) Birth(parentName string, childName string) {
230
+
231
+ func (this *ThroneInheritance) Birth(parentName string, childName string) {
229
232
this.g[parentName] = append(this.g[parentName], childName)
230
233
}
231
234
232
- func (this *ThroneInheritance) Death(name string) {
235
+
236
+ func (this *ThroneInheritance) Death(name string) {
233
237
this.dead[name] = true
234
238
}
235
239
236
- func (this *ThroneInheritance) GetInheritanceOrder() []string {
237
- var dfs func(x string)
238
- ans := []string{}
239
240
241
+ func (this *ThroneInheritance) GetInheritanceOrder() (ans []string) {
242
+ var dfs func(string)
240
243
dfs = func(x string) {
241
244
if !this.dead[x] {
242
245
ans = append(ans, x)
@@ -246,9 +249,10 @@ func (this *ThroneInheritance) GetInheritanceOrder() []string {
246
249
}
247
250
}
248
251
dfs(this.king)
249
- return ans
252
+ return
250
253
}
251
254
255
+
252
256
/**
253
257
* Your ThroneInheritance object will be instantiated and called as such:
254
258
* obj := Constructor(kingName);
@@ -258,6 +262,98 @@ func (this *ThroneInheritance) GetInheritanceOrder() []string {
258
262
*/
259
263
```
260
264
265
+ ``` ts
266
+ class ThroneInheritance {
267
+ private king: string ;
268
+ private dead: Set <string > = new Set ();
269
+ private g: Map <string , string []> = new Map ();
270
+
271
+ constructor (kingName : string ) {
272
+ this .king = kingName ;
273
+ }
274
+
275
+ birth(parentName : string , childName : string ): void {
276
+ this .g .set (parentName , this .g .get (parentName ) || []);
277
+ this .g .get (parentName )! .push (childName );
278
+ }
279
+
280
+ death(name : string ): void {
281
+ this .dead .add (name );
282
+ }
283
+
284
+ getInheritanceOrder(): string [] {
285
+ const ans: string [] = [];
286
+ const dfs = (x : string ) => {
287
+ if (! this .dead .has (x )) {
288
+ ans .push (x );
289
+ }
290
+ for (const y of this .g .get (x ) || []) {
291
+ dfs (y );
292
+ }
293
+ };
294
+ dfs (this .king );
295
+ return ans ;
296
+ }
297
+ }
298
+
299
+ /**
300
+ * Your ThroneInheritance object will be instantiated and called as such:
301
+ * var obj = new ThroneInheritance(kingName)
302
+ * obj.birth(parentName,childName)
303
+ * obj.death(name)
304
+ * var param_3 = obj.getInheritanceOrder()
305
+ */
306
+ ```
307
+
308
+ ``` cs
309
+ public class ThroneInheritance {
310
+ private string king ;
311
+ private HashSet <string > dead = new HashSet <string >();
312
+ private Dictionary <string , List <string >> g = new Dictionary <string , List <string >>();
313
+ private List <string > ans = new List <string >();
314
+
315
+ public ThroneInheritance (string kingName ) {
316
+ king = kingName ;
317
+ }
318
+
319
+ public void Birth (string parentName , string childName ) {
320
+ if (! g .ContainsKey (parentName )) {
321
+ g [parentName ] = new List <string >();
322
+ }
323
+ g [parentName ].Add (childName );
324
+ }
325
+
326
+ public void Death (string name ) {
327
+ dead .Add (name );
328
+ }
329
+
330
+ public IList <string > GetInheritanceOrder () {
331
+ ans .Clear ();
332
+ DFS (king );
333
+ return ans ;
334
+ }
335
+
336
+ private void DFS (string x ) {
337
+ if (! dead .Contains (x )) {
338
+ ans .Add (x );
339
+ }
340
+ if (g .ContainsKey (x )) {
341
+ foreach (string y in g [x ]) {
342
+ DFS (y );
343
+ }
344
+ }
345
+ }
346
+ }
347
+
348
+ /**
349
+ * Your ThroneInheritance object will be instantiated and called as such:
350
+ * ThroneInheritance obj = new ThroneInheritance(kingName);
351
+ * obj.Birth(parentName,childName);
352
+ * obj.Death(name);
353
+ * IList<string > param_3 = obj.GetInheritanceOrder();
354
+ */
355
+ ```
356
+
261
357
<!-- tabs:end -->
262
358
263
359
<!-- end -->
0 commit comments