Skip to content

Latest commit

 

History

History
39 lines (31 loc) · 1004 Bytes

1600.-throne-inheritance.md

File metadata and controls

39 lines (31 loc) · 1004 Bytes

1600. Throne Inheritance

class ThroneInheritance:
    ## 阅读理解不过关,
    ## 想到就是多叉树,就是图还是不会 
    def __init__(self, kingName: str):
        self.kname = kingName
        self.dead = set()
        self.sons = collections.defaultdict(list)
        self.ans = []

    def birth(self, parentName: str, childName: str) -> None:
        self.sons[parentName].append(childName)

    def death(self, name: str) -> None:
        self.dead.add(name)

    def getInheritanceOrder(self) -> List[str]:
        self.ans = []
        self.dfs(self.kname)
        return self.ans

    def dfs(self,name):
        if name in self.dead:
            pass
        else:
            self.ans.append(name)
        for i in self.sons[name]:
            self.dfs(i)


# Your ThroneInheritance object will be instantiated and called as such:
# obj = ThroneInheritance(kingName)
# obj.birth(parentName,childName)
# obj.death(name)
# param_3 = obj.getInheritanceOrder()