mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 08:27:30 +08:00
42 lines
953 B
Go
42 lines
953 B
Go
package leetcode
|
|
|
|
type ThroneInheritance struct {
|
|
king string
|
|
edges map[string][]string
|
|
dead map[string]bool
|
|
}
|
|
|
|
func Constructor(kingName string) (t ThroneInheritance) {
|
|
return ThroneInheritance{kingName, map[string][]string{}, map[string]bool{}}
|
|
}
|
|
|
|
func (t *ThroneInheritance) Birth(parentName, childName string) {
|
|
t.edges[parentName] = append(t.edges[parentName], childName)
|
|
}
|
|
|
|
func (t *ThroneInheritance) Death(name string) {
|
|
t.dead[name] = true
|
|
}
|
|
|
|
func (t *ThroneInheritance) GetInheritanceOrder() (res []string) {
|
|
var preorder func(string)
|
|
preorder = func(name string) {
|
|
if !t.dead[name] {
|
|
res = append(res, name)
|
|
}
|
|
for _, childName := range t.edges[name] {
|
|
preorder(childName)
|
|
}
|
|
}
|
|
preorder(t.king)
|
|
return
|
|
}
|
|
|
|
/**
|
|
* Your ThroneInheritance object will be instantiated and called as such:
|
|
* obj := Constructor(kingName);
|
|
* obj.Birth(parentName,childName);
|
|
* obj.Death(name);
|
|
* param_3 := obj.GetInheritanceOrder();
|
|
*/
|