-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1600_throneInheritance.cpp
57 lines (47 loc) · 1.3 KB
/
1600_throneInheritance.cpp
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
class ThroneInheritance {
struct Node{
string name;
vector<Node*> children;
Node(string name){
this->name=name;
}
};
public:
Node* root;
unordered_set<string> dead;
unordered_map<string,Node*> mp;
ThroneInheritance(string kingName) {
root = new Node(kingName);
mp[kingName] = root;
}
void birth(string parentName, string childName) {
Node *parent = mp[parentName];
Node *child = new Node(childName);
mp[childName] = child;
(parent->children).push_back(child);
}
void death(string name) {
dead.insert(name);
}
void preorder(vector<string> &res, Node *node, unordered_set<string> &dead){
if(!node) return;
if(dead.find(node->name)==dead.end()){
res.push_back(node->name);
}
for(auto &it: node->children){
preorder(res, it, dead);
}
}
vector<string> getInheritanceOrder() {
vector<string> res;
preorder(res, root, dead);
return res;
}
};
/**
* Your ThroneInheritance object will be instantiated and called as such:
* ThroneInheritance* obj = new ThroneInheritance(kingName);
* obj->birth(parentName,childName);
* obj->death(name);
* vector<string> param_3 = obj->getInheritanceOrder();
*/