mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-07 01:44:56 +08:00
Add solution 1600, change dir 0167、0303、0304、0307、0653、1017
This commit is contained in:
41
leetcode/1600.Throne-Inheritance/1600. Throne Inheritance.go
Normal file
41
leetcode/1600.Throne-Inheritance/1600. Throne Inheritance.go
Normal 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();
|
||||
*/
|
Reference in New Issue
Block a user