-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.go
43 lines (37 loc) · 943 Bytes
/
Solution.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
type ThroneInheritance struct {
g map[string][]string
dead map[string]bool
king string
}
func Constructor(kingName string) ThroneInheritance {
g := map[string][]string{}
dead := map[string]bool{}
return ThroneInheritance{g, dead, kingName}
}
func (this *ThroneInheritance) Birth(parentName string, childName string) {
this.g[parentName] = append(this.g[parentName], childName)
}
func (this *ThroneInheritance) Death(name string) {
this.dead[name] = true
}
func (this *ThroneInheritance) GetInheritanceOrder() []string {
var dfs func(x string)
ans := []string{}
dfs = func(x string) {
if !this.dead[x] {
ans = append(ans, x)
}
for _, y := range this.g[x] {
dfs(y)
}
}
dfs(this.king)
return ans
}
/**
* Your ThroneInheritance object will be instantiated and called as such:
* obj := Constructor(kingName);
* obj.Birth(parentName,childName);
* obj.Death(name);
* param_3 := obj.GetInheritanceOrder();
*/