-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSumOfDistancesInTree834.java
61 lines (54 loc) · 2.27 KB
/
SumOfDistancesInTree834.java
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
public class SumOfDistancesInTree834 {
/*
* There is an undirected connected tree with n nodes labeled from 0 to n - 1 and n - 1 edges.
*
* You are given the integer n and the array edges where edges[i] = [ai, bi] indicates that there is
an edge between nodes ai and bi in the tree.
* Return an array answer of length n where answer[i] is the sum of the distances between the ith node in the tree and all other nodes.
*
*/
class Solution {
int[][] graph;
int[] count;
int[] res;
int N;
public int[] sumOfDistancesInTree(int N, int[][] edges) {
this.N = N;
this.res = new int[N];
this.graph = new int[N][];
this.count = new int[N];
for (int[] e : edges) {
++count[e[0]];
++count[e[1]];
}
for (int i = 0; i < N; i++) {
graph[i] = new int[count[i]];
}
for (int[] e : edges) {
graph[e[0]][--count[e[0]]] = e[1];
graph[e[1]][--count[e[1]]] = e[0];
}
dfs1(0, -1);
dfs2(0, -1);
return res;
}
public void dfs1(int cur, int parent) {
count[cur] = 1;
for (int child : graph[cur]) {
if (child != parent) {
dfs1(child, cur);
count[cur] += count[child];
res[cur] += res[child] + count[child];
}
}
}
public void dfs2(int cur, int parent) {
for (int child : graph[cur]) {
if (child != parent) {
res[child] = res[cur] + N - 2 * count[child];
dfs2(child, cur);
}
}
}
}
}