-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththroneInheritance.py
More file actions
36 lines (28 loc) · 1005 Bytes
/
throneInheritance.py
File metadata and controls
36 lines (28 loc) · 1005 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/throne-inheritance/
# Author: Miao Zhang
# Date: 2021-05-19
class ThroneInheritance:
def __init__(self, kingName: str):
self.king = kingName
self.order = collections.defaultdict(list)
self.dead = set()
def birth(self, parentName: str, childName: str) -> None:
self.order[parentName].append(childName)
def death(self, name: str) -> None:
self.dead.add(name)
def getInheritanceOrder(self) -> List[str]:
self.res = []
def dfs(king: str) -> None:
if king not in self.dead:
self.res.append(king)
for child in self.order[king]:
dfs(child)
dfs(self.king)
return self.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()