generated from Suvechchhaa/HacktoberFest-DSA2022
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsubtreeDifference.Java
72 lines (57 loc) · 1.41 KB
/
subtreeDifference.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
62
63
64
65
66
67
68
69
70
71
72
import java.util.ArrayList;
class Graph
{
static int res;
static void dfs (int u, int parent, int totalSum,
ArrayList < Integer >[]edge, int subtree[])
{
int sum = subtree[u];
for (int i = 0; i < edge[u].size (); i++)
{
int v = edge[u].get (i);
if (v != parent)
{
dfs (v, u, totalSum, edge, subtree);
sum += subtree[v];
}
}
subtree[u] = sum;
if (u != 0 && Math.abs (totalSum - 2 * sum) < res)
res = Math.abs (totalSum - 2 * sum);
}
static int getMinSubtreeSumDifference (int vertex[], int[][]edges, int N)
{
int totalSum = 0;
int[] subtree = new int[N];
for (int i = 0; i < N; i++)
{
subtree[i] = vertex[i];
totalSum += vertex[i];
}
@SuppressWarnings ("unchecked")
ArrayList < Integer >[]edge = new ArrayList[N];
for (int i = 0; i < N; i++)
{
edge[i] = new ArrayList <> ();
}
for (int i = 0; i < N - 1; i++)
{
edge[edges[i][0]].add (edges[i][1]);
edge[edges[i][1]].add (edges[i][0]);
}
dfs (0, -1, totalSum, edge, subtree);
return res;
}
public static void main (String[]args)
{
res = Integer.MAX_VALUE;
int[] vertex = { 4, 2, 1, 6, 3, 5, 2 };
int[][] edges = { {0, 1}, {0, 2},
{0, 3}, {2, 4},
{2, 5}, {3, 6}
};
int N = vertex.length;
System.out.println (getMinSubtreeSumDifference (vertex, edges, N));
}
}
//Subtree Difference in Java