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(); */