forked from fengvyi/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
834. Sum of Distances in Tree.cpp
35 lines (35 loc) · 1.21 KB
/
834. Sum of Distances in Tree.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
class Solution {
public:
vector<int> sumOfDistancesInTree(int N, vector<vector<int>>& edges) {
vector<vector<int>>g(N, vector<int>());
for(auto& e: edges){
g[e[0]].push_back(e[1]);
g[e[1]].push_back(e[0]);
}
vector<int>res(N), child(N), visited(N);
dfs(g, res, child, 0, 0, visited);
for(auto& x: visited) x = 0;
dfs(g, res, child, 0, visited, N);
return res;
}
// Sum of the distances of node 0
void dfs(vector<vector<int>>& g, vector<int>& res, vector<int>& child, int len, int root, vector<int>& visited){
visited[root] = 1;
res[0] += len++;
for(auto& x: g[root]){
if(visited[x]) continue;
dfs(g, res, child, len, x, visited);
child[root] += child[x];
}
child[root] += 1;
}
// Sum of the distances of node 1 to N - 1
void dfs(vector<vector<int>>& g, vector<int>& res, vector<int>& child, int root, vector<int>& visited, int N){
visited[root] = 1;
for(auto& x: g[root]){
if(visited[x]) continue;
res[x] = res[root] - child[x] + N - child[x];
dfs(g, res, child, x, visited, N);
}
}
};