-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountSubTrees.cpp
More file actions
24 lines (24 loc) · 795 Bytes
/
Copy pathcountSubTrees.cpp
File metadata and controls
24 lines (24 loc) · 795 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
vector<int> countSubTrees(int n, vector<vector<int>>& edges, string labels) {
vector<vector<int>> graph(n);
for (auto &v: edges) {
graph[v[0]].push_back(v[1]);
graph[v[1]].push_back(v[0]);
}
vector<int> ans(n, 0);
std::function<vector<int>(int, int)> dfs = [&](int node, int p) {
vector<int> cnt(26, 0);
for (int next: graph[node]) {
if (next == p) continue;
vector<int> temp = dfs(next, node);
for (int i = 0; i < cnt.size(); ++i)
cnt[i] += temp[i];
}
ans[node] = ++cnt[labels[node] - 'a'];
return std::move(cnt);
};
dfs(0, -1);
return ans;
}
};