Add solution 1600, change dir 0167、0303、0304、0307、0653、1017

This commit is contained in:
YDZ
2021-06-20 13:44:46 +08:00
parent fe99f9d7d5
commit c28e1c0267
39 changed files with 758 additions and 416 deletions

View File

@ -0,0 +1,41 @@
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();
*/