1
# Runtime: 1338 ms (Top 35.29%) | Memory: 68.8 MB (Top 82.35%)4
class ThroneInheritance:6
def __init__(self, kingName: str):7
# Taking kingName as root10
# notDead will hold all the people who are alive and their level number12
self.alive[kingName] = 014
# hold edges existing in our graph15
self.edges = {self.root: []}17
def birth(self, parentName: str, childName: str) -> None:18
# birth --> new child so update alive19
self.alive[childName] = self.alive[parentName] + 121
# add parent to child edges in the edges dictionary22
if parentName in self.edges:23
self.edges[parentName].append(childName)24
if childName not in self.edges:25
self.edges[childName] = []27
if childName not in self.edges:28
self.edges[childName] = []29
self.edges[parentName] = [childName]31
def death(self, name: str) -> None:32
# removing the dead people from alive map35
def getInheritanceOrder(self) -> List[str]:39
def dfs(cur, parent=-1):42
# current person available in alive then only add in hierarchy46
# traverse all the children of current node47
for i in self.edges[cur]: