-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1600.java
72 lines (63 loc) · 2.34 KB
/
_1600.java
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.fishercoder.solutions.secondthousand;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class _1600 {
public static class Solution1 {
/*
* My completely original solution:
* 1. use a tree structure to represent the king family;
* 2. then use preorder traversal to find the inheritance order;
* 3. use a map to quickly find the corresponding node in the tree based on the name since all names are distinct
*/
public static class Person {
List<Person> children;
String name;
boolean isAlive;
public Person(String kingName) {
this.name = kingName;
this.children = new ArrayList<>();
this.isAlive = true;
}
}
public static class ThroneInheritance {
Person king;
Map<String, Person> map;
String kingName;
public ThroneInheritance(String kingName) {
king = new Person(kingName);
map = new HashMap<>();
this.kingName = kingName;
map.put(kingName, king);
}
public void birth(String parentName, String childName) {
Person parentNode = map.getOrDefault(parentName, new Person(parentName));
Person child = new Person(childName);
parentNode.children.add(child);
map.put(parentName, parentNode);
map.put(childName, child);
}
public void death(String name) {
Person deadPerson = map.get(name);
deadPerson.isAlive = false;
map.put(name, deadPerson);
}
public List<String> getInheritanceOrder() {
return preorder(map.get(this.kingName), new ArrayList<>());
}
private List<String> preorder(Person person, List<String> list) {
if (person == null) {
return list;
}
if (person.isAlive) {
list.add(person.name);
}
for (Person child : person.children) {
preorder(child, list);
}
return list;
}
}
}
}