1 Star 0 Fork 0

catyMap / AlgorithmNote

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
ThroneInheritance_official.go 1014 Bytes
一键复制 编辑 原始数据 按行查看 历史
dogemap 提交于 2021-06-21 08:19 . 6-21更新
package main
import "crypto/x509/pkix"
// 官方版记忆手写, 使用map模拟多叉树
type ThroneInheritance struct {
king string
familyBook map[string][]string
deadBook map[string]bool
}
func Constructor(kingName string) ThroneInheritance {
return ThroneInheritance{
kingName,
map[string][]string{
kingName: make([]string, 0, 0),
},
make(map[string]bool),
}
}
func (this *ThroneInheritance) Birth(parentName string, childName string) {
this.familyBook[parentName] = append(this.familyBook[parentName], childName)
this.familyBook[childName] = make([]string, 0, 0)
}
func (this *ThroneInheritance) Death(name string) {
this.deadBook[name] = true
}
func (this *ThroneInheritance) GetInheritanceOrder() []string {
var dfs func(name string)
res := make([]string, 0, 0)
dfs = func(name string) {
if !this.deadBook[name] {
res = append(res, name)
}
childs := this.familyBook[name]
for _, childName := range childs {
dfs(childName)
}
}
dfs(this.king)
return res
}
Go
1
https://gitee.com/dogemap/algorithm-note.git
git@gitee.com:dogemap/algorithm-note.git
dogemap
algorithm-note
AlgorithmNote
dc486f96f6c1

搜索帮助