class ThroneInheritance {
Map<String, List<String>> edges;
Set<String> dead;
String king;
public ThroneInheritance(String kingName) {
edges = new HashMap<String, List<String>>();
dead = new HashSet<String>();
king = kingName;
}
public void birth(String parentName, String childName) {
List<String> children = edges.getOrDefault(parentName, new ArrayList<String>());
children.add(childName);
edges.put(parentName, children);
}
public void death(String name) {
dead.add(name);
}
public List<String> getInheritanceOrder() {
List<String> ans = new ArrayList<String>();
preorder(ans, king);
return ans;
}
private void preorder(List<String> ans, String name) {
if (!dead.contains(name)) {
ans.add(name);
}
List<String> children = edges.getOrDefault(name, new ArrayList<String>());
for (String childName : children) {
preorder(ans, childName);
}
}
}
/**
* Your ThroneInheritance object will be instantiated and called as such:
* ThroneInheritance obj = new ThroneInheritance(kingName);
* obj.birth(parentName,childName);
* obj.death(name);
* List<String> param_3 = obj.getInheritanceOrder();
*/
class ThroneInheritance:
def __init__(self, kingName: str):
self.edges = defaultdict(list)
self.dead = set()
self.king = kingName
def birth(self, parentName: str, childName: str) -> None:
self.edges[parentName].append(childName)
def death(self, name: str) -> None:
self.dead.add(name)
def getInheritanceOrder(self) -> List[str]:
ans = list()
def preorder(name: str) -> None:
if name not in self.dead:
ans.append(name)
if name in self.edges:
for childName in self.edges[name]:
preorder(childName)
preorder(self.king)
return ans
# Your ThroneInheritance object will be instantiated and called as such:
# obj = ThroneInheritance(kingName)
# obj.birth(parentName,childName)
# obj.death(name)
# param_3 = obj.getInheritanceOrder()