forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
number-of-nodes-in-the-sub-tree-with-the-same-label.cpp
82 lines (78 loc) · 2.69 KB
/
number-of-nodes-in-the-sub-tree-with-the-same-label.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Time: O(n)
// Space: O(h)
class Solution {
public:
vector<int> countSubTrees(int n, vector<vector<int>>& edges, string labels) {
vector<vector<int>> adj(n);
for (const auto& edge : edges) {
adj[edge[0]].emplace_back(edge[1]);
adj[edge[1]].emplace_back(edge[0]);
}
vector<int> result(n);
iter_dfs(labels, adj, 0, -1, &result);
return result;
}
private:
void iter_dfs(
const string& labels,
const vector<vector<int>>& adj,
int node, int parent, vector<int> *result) {
using RET = vector<int>;
RET ret(26);
vector<tuple<int, int, int, int, shared_ptr<RET>, RET *>> stk = {{1, node, -1, -1, nullptr, &ret}};
while (!stk.empty()) {
const auto [step, node, parent, i, ret2, ret] = stk.back(); stk.pop_back();
if (step == 1) {
stk.emplace_back(4, node, -1, -1, nullptr, ret);
stk.emplace_back(2, node, parent, adj[node].size() - 1, nullptr, ret);
} else if (step == 2) {
if (i < 0 || adj[node][i] == parent) {
continue;
}
const auto& ret2 = make_shared<vector<int>>(26);
stk.emplace_back(2, node, parent, i - 1, nullptr, ret);
stk.emplace_back(3, -1, -1, -1, ret2, ret);
stk.emplace_back(1, adj[node][i], node, -1, nullptr, ret2.get());
} else if (step == 3) {
for (int k = 0; k < ret2->size(); ++k) {
(*ret)[k] += (*ret2)[k];
}
} else {
(*result)[node] = ++(*ret)[labels[node] - 'a'];
}
}
}
};
// Time: O(n)
// Space: O(h)
class Solution_Recu {
public:
vector<int> countSubTrees(int n, vector<vector<int>>& edges, string labels) {
vector<vector<int>> adj(n);
for (const auto& edge : edges) {
adj[edge[0]].emplace_back(edge[1]);
adj[edge[1]].emplace_back(edge[0]);
}
vector<int> result(n);
dfs(labels, adj, 0, -1, &result);
return result;
}
private:
vector<int> dfs(
const string& labels,
const vector<vector<int>>& adj,
int node, int parent, vector<int> *result) {
vector<int> count(26);
for (const auto& child : adj[node]) {
if (child == parent) {
continue;
}
const auto& new_count = dfs(labels, adj, child, node, result);
for (int k = 0; k < new_count.size(); ++k) {
count[k] += new_count[k];
}
}
(*result)[node] = ++count[labels[node] - 'a'];
return count;
}
};