-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsumofDistancesinTree.cpp
More file actions
45 lines (41 loc) · 1.14 KB
/
sumofDistancesinTree.cpp
File metadata and controls
45 lines (41 loc) · 1.14 KB
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
// Source: https://leetcode.com/problems/sum-of-distances-in-tree/
// Author: Miao Zhang
// Date: 2021-03-16
class Solution {
public:
vector<int> sumOfDistancesInTree(int N, vector<vector<int>>& edges) {
N_ = N;
res = vector<int>(N, 0);
count = vector<int>(N, 1);
graph = vector<vector<int>>(N);
for (auto& edge: edges) {
graph[edge[0]].push_back(edge[1]);
graph[edge[1]].push_back(edge[0]);
}
dfs(0, -1);
dfs2(0, -1);
return res;
}
private:
vector<int> res;
vector<int> count;
vector<vector<int>> graph;
int N_;
void dfs(int node, int parent) {
for (int child: graph[node]) {
if (child != parent) {
dfs(child, node);
count[node] += count[child];
res[node] += res[child] + count[child];
}
}
}
void dfs2(int node, int parent) {
for (int child: graph[node]) {
if (child != parent) {
res[child] = res[node] - count[child] + N_ - count[child];
dfs2(child, node);
}
}
}
};