A kingdom consists of a king, his children, his grandchildren, and so on. Every once in a while, someone in the family dies or a child is born.

The kingdom has a well-defined order of inheritance that consists of the king as the first member. Let's define the recursive function Successor(x, curOrder), which given a person x and the inheritance order so far, returns who should be the next person after x in the order of inheritance.

Successor(x, curOrder):
    if x has no children or all of x's children are in curOrder:
        if x is the king return null
        else return Successor(x's parent, curOrder)
    else return x's oldest child who's not in curOrder


For example, assume we have a kingdom that consists of the king, his children Alice and Bob (Alice is older than Bob), and finally Alice's son Jack.


	In the beginning, curOrder will be ["king"].
	Calling Successor(king, curOrder) will return Alice, so we append to curOrder to get ["king", "Alice"].
	Calling Successor(Alice, curOrder) will return Jack, so we append to curOrder to get ["king", "Alice", "Jack"].
	Calling Successor(Jack, curOrder) will return Bob, so we append to curOrder to get ["king", "Alice", "Jack", "Bob"].
	Calling Successor(Bob, curOrder) will return null. Thus the order of inheritance will be ["king", "Alice", "Jack", "Bob"].


Using the above function, we can always obtain a unique order of inheritance.

Implement the ThroneInheritance class:


	ThroneInheritance(string kingName) Initializes an object of the ThroneInheritance class. The name of the king is given as part of the constructor.
	void birth(string parentName, string childName) Indicates that parentName gave birth to childName.
	void death(string name) Indicates the death of name. The death of the person doesn't affect the Successor function nor the current inheritance order. You can treat it as just marking the person as dead.
	string[] getInheritanceOrder() Returns a list representing the current order of inheritance excluding dead people.


&nbsp;
Example 1:

Input
["ThroneInheritance", "birth", "birth", "birth", "birth", "birth", "birth", "getInheritanceOrder", "death", "getInheritanceOrder"]
[["king"], ["king", "andy"], ["king", "bob"], ["king", "catherine"], ["andy", "matthew"], ["bob", "alex"], ["bob", "asha"], [null], ["bob"], [null]]
Output
[null, null, null, null, null, null, null, ["king", "andy", "matthew", "bob", "alex", "asha", "catherine"], null, ["king", "andy", "matthew", "alex", "asha", "catherine"]]

Explanation
ThroneInheritance t= new ThroneInheritance("king"); // order: king
t.birth("king", "andy"); // order: king &gt; andy
t.birth("king", "bob"); // order: king &gt; andy &gt; bob
t.birth("king", "catherine"); // order: king &gt; andy &gt; bob &gt; catherine
t.birth("andy", "matthew"); // order: king &gt; andy &gt; matthew &gt; bob &gt; catherine
t.birth("bob", "alex"); // order: king &gt; andy &gt; matthew &gt; bob &gt; alex &gt; catherine
t.birth("bob", "asha"); // order: king &gt; andy &gt; matthew &gt; bob &gt; alex &gt; asha &gt; catherine
t.getInheritanceOrder(); // return ["king", "andy", "matthew", "bob", "alex", "asha", "catherine"]
t.death("bob"); // order: king &gt; andy &gt; matthew &gt; bob &gt; alex &gt; asha &gt; catherine
t.getInheritanceOrder(); // return ["king", "andy", "matthew", "alex", "asha", "catherine"]


&nbsp;
Constraints:


	1 &lt;= kingName.length, parentName.length, childName.length, name.length &lt;= 15
	kingName, parentName, childName, and name consist of lowercase English letters only.
	All arguments childName and kingName are distinct.
	All name arguments of death will be passed to either the constructor or as childName to birth first.
	For each call to&nbsp;birth(parentName, childName), it is guaranteed that&nbsp;parentName is alive.
	At most 105 calls will be made to birth and death.
	At most 10 calls will be made to getInheritanceOrder.


class ThroneInheritance:

    def __init__(self, kingName: str):
        # Taking kingName as root
        self.root = kingName

        # notDead will hold all the people who are alive and their level number
        self.alive = {}
        self.alive[kingName] = 0

        # hold edges existing in our graph
        self.edges = {self.root:[]}

    def birth(self, parentName: str, childName: str) -> None:
        # birth --> new child so update alive
        self.alive[childName] = self.alive[parentName]+1

        # add parent to child edges in the edges dictionary
        if parentName in self.edges:
            self.edges[parentName].append(childName)
            if childName not in self.edges:
                self.edges[childName] = []
        else:
            if childName not in self.edges:
                self.edges[childName] = []
            self.edges[parentName] = [childName]

    def death(self, name: str) -> None:
        # removing the dead people from alive map
        del self.alive[name]

    def getInheritanceOrder(self) -> List[str]:

        hierarchy = []
        def dfs(cur,parent=-1):
            nonlocal hierarchy

            # current person available in alive then only add in hierarchy
            if cur in self.alive:
                hierarchy.append(cur)

            # traverse all the children of current node
            for i in self.edges[cur]:
                if i!=parent:
                    dfs(i,cur)
        dfs(self.root)
        return hierarchy
class Tree{
    List<Tree>child;
    String name;
    public Tree(String name,List<Tree>child){
        this.name=name;
        this.child=child;
    }
}
class ThroneInheritance {
    private Set<String>death;
    private Tree tree;
    private Map<String,Tree>addtoTree;
    public ThroneInheritance(String kingName) {
      death=new HashSet<>();  
      tree=new Tree(kingName,new ArrayList());
      addtoTree=new HashMap();
      addtoTree.put(kingName,tree);   
    }
    
    public void birth(String parentName, String childName) {
           Tree tmp =addtoTree.get(parentName);
           Tree childtree=new Tree(childName,new ArrayList());
           tmp.child.add(childtree);
            addtoTree.put( childName,childtree); 
    }
    
    public void death(String name) {
        death.add(name);
    }
    
    public List<String> getInheritanceOrder() {
        List<String>ans=new ArrayList<>();
        preOreder(tree,ans,death);
        return ans;
    }
    
    void preOreder(Tree n,List<String>ans,Set<String>death){
        if(n==null)return;
        if(!death.contains(n.name))ans.add(n.name);
         for(Tree name:n.child){
            preOreder(name,ans,death);
        }
    }
}
class ThroneInheritance {
public:
    ThroneInheritance(string kingName) {
        curr_king = kingName;
    }
    
    void birth(string parentName, string childName) {
        children[parentName].push_back(childName);
    }
    
    void death(string name) {
        dead.insert(name);
    }
    
    void rec(string parent) {
        if (!dead.count(parent)) inheritance.push_back(parent);
        for (auto child : children[parent])
            rec(child);
    }
    
    vector<string> getInheritanceOrder() {
        inheritance = {};
        rec(curr_king);
        return inheritance;
    }
    
private:
    unordered_map<string, vector<string>> children;
    vector<string> inheritance;
    unordered_set<string> dead;
    string curr_king;
};
var bloodList = function(name, parent = null) {
    return {
        name,
        children: []
    };
}

var ThroneInheritance = function(kingName) {
    this.nameMap = new Map();
    this.nameMap.set(kingName, bloodList(kingName));
    this.king = kingName;
    this.deadList = new Set();
};

/**
 * @param {string} parentName
 * @param {string} childName
 * @return {void}
 */
ThroneInheritance.prototype.birth = function(parentName, childName) {
    const parent = this.nameMap.get(parentName);
    const childId = bloodList(childName, parent);
    this.nameMap.set(childName, childId);
    parent.children.push(childId);
};

/**
 * @param {string} name
 * @return {void}
 */
ThroneInheritance.prototype.death = function(name) {
    this.deadList.add(name);
};

/**
 * @return {string[]}
 */
function updateList(list, nameId, deadList) {
    if (!deadList.has(nameId.name))
        list.push(nameId.name);

    for (let child of nameId.children) {
        updateList(list, child, deadList);
    }
}

ThroneInheritance.prototype.getInheritanceOrder = function() {
    let list = [];
    updateList(list, this.nameMap.get(this.king), this.deadList);
    return list;
};
Throne Inheritance