1600. Throne Inheritance

Обновлено: 2024-03-12
1 мин
[]

Содержание

LeetCode problem 1600

class ThroneInheritance:
    def __init__(self, kingName: str):
        self.g = defaultdict(list)
        self.dead = set()
        self.king = kingName

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

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

    def getInheritanceOrder(self) -> List[str]:
        def dfs(x):
            if x not in self.dead:
                res.append(x)
            for y in self.g[x]:
                dfs(y)

        res = []
        dfs(self.king)
        return res


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