1600. Throne Inheritance

Difficulty:
Related Topics:
Similar Questions:

Problem

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.

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

Implement the ThroneInheritance class:

  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 > andy
t.birth("king", "bob"); // order: king > andy > bob
t.birth("king", "catherine"); // order: king > andy > bob > catherine
t.birth("andy", "matthew"); // order: king > andy > matthew > bob > catherine
t.birth("bob", "alex"); // order: king > andy > matthew > bob > alex > catherine
t.birth("bob", "asha"); // order: king > andy > matthew > bob > alex > asha > catherine
t.getInheritanceOrder(); // return ["king", "andy", "matthew", "bob", "alex", "asha", "catherine"]
t.death("bob"); // order: king > andy > matthew > bob > alex > asha > catherine
t.getInheritanceOrder(); // return ["king", "andy", "matthew", "alex", "asha", "catherine"]

  Constraints:

Solution (Java)

class ThroneInheritance {
    private String king;
    private HashMap<String, LinkedHashSet<String>> graph;
    private HashSet<String> isDead;

    public ThroneInheritance(String kingName) {
        king = kingName;
        graph = new HashMap<>();
        isDead = new HashSet<>();
        graph.put(kingName, new LinkedHashSet<>());
    }

    public void birth(String parentName, String childName) {
        graph.putIfAbsent(parentName, new LinkedHashSet<>());
        graph.get(parentName).add(childName);
    }

    public void death(String name) {
        isDead.add(name);
    }

    public List<String> getInheritanceOrder() {
        List<String> inheritance = new ArrayList<>();
        HashSet<String> visited = new HashSet<>();
        dfs(graph, king, inheritance, visited);
        return inheritance;
    }

    public void dfs(
            Map<String, LinkedHashSet<String>> graph,
            String src,
            List<String> l,
            Set<String> visited) {
        visited.add(src);
        if (!isDead.contains(src)) {
            l.add(src);
        }
        if (!graph.containsKey(src)) {
            return;
        }
        for (String s : graph.get(src)) {
            if (!visited.contains(s)) {
                dfs(graph, s, l, visited);
            }
        }
    }
}

/**
 * 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();
 */

Explain:

nope.

Complexity: