This repository has been archived by the owner on Oct 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create SumOfDistancesInTree.java (#536)
- Loading branch information
Showing
1 changed file
with
57 additions
and
0 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
Programming/Java/Sum Of Distances in Tree/SumOfDistancesInTree.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
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. | ||
Input: n = 6, edges = [[0,1],[0,2],[2,3],[2,4],[2,5]] | ||
Output: [8,12,6,10,10,10] | ||
*/ | ||
|
||
public int[] sumOfDistancesInTree(int N, int[][] edges) { | ||
if (N == 1) { | ||
return new int[N]; | ||
} | ||
if (N == 2) { | ||
return new int[]{1, 1}; | ||
} | ||
|
||
List<int[]>[] graph = new ArrayList[N]; | ||
for (int i = 0; i < N; i++) { | ||
graph[i] = new ArrayList<>(); | ||
} | ||
for (int i = 0; i < edges.length; i++) { | ||
// [0] = to [1] = sum [2] = num | ||
graph[edges[i][0]].add(new int[]{edges[i][1], 0, 0}); | ||
graph[edges[i][1]].add(new int[]{edges[i][0], 0, 0}); | ||
} | ||
|
||
int[] result = new int[N]; | ||
boolean[] seen = new boolean[N]; | ||
for (int i = 0; i < N; i++) { | ||
result[i] = dfs(graph, i, seen)[0]; | ||
} | ||
return result; | ||
} | ||
|
||
private int[] dfs(List<int[]>[] graph, int i, boolean[] seen) { | ||
seen[i] = true; | ||
int sum = 0; | ||
int num = 1; | ||
for (int[] adj : graph[i]) { | ||
if (!seen[adj[0]]) { | ||
if (adj[1] == 0 && adj[2] == 0) { | ||
int[] res = dfs(graph, adj[0], seen); | ||
adj[1] = res[0]; | ||
adj[2] = res[1]; | ||
} | ||
sum += (adj[1] + adj[2]); | ||
num += adj[2]; | ||
} | ||
} | ||
seen[i] = false; | ||
return new int[]{sum, num}; | ||
} |