• + 2 comments
    class Result {
    
    static List<List<Integer>> graph;
    static List<Integer> data;
    static int root,res,total;
    public static void create(List<List<Integer>> edges, int n){
    for (int i = 0; i <= n; i++) {
    graph.add(new ArrayList<Integer>()); // Change to LinkedList<Integer>()
    }
    
    for(List<Integer> e : edges) {
    graph.get(e.get(0)).add(e.get(1));
    graph.get(e.get(1)).add(e.get(0));
    }
    
    
    }
    
    private static int dfs(int r, boolean[] vis){
    vis[r]=true;
    if(graph.get(r).size() == 0) return 0;
    
    int sum =0;
    for(int i : graph.get(r)){
    if(!vis[i])
    sum+=dfs(i,vis);
    }
    
    sum+=data.get(r-1);
    res=Math.min(res,Math.abs(total-sum*2));
    return sum;
    
    
    } 
    public static int cutTheTree(List<Integer> data, List<List<Integer>> edges) {
    // Write your code here
    int s = 0;
    graph = new ArrayList<>();
    root =-1;
    res =Integer.MAX_VALUE;
    for(int i : data)
    s+=i;
    total = s;
    create(edges,data.size());
    Result.data = data;
    int v =dfs(1,new boolean[data.size()+2]);
    return res;
            
            
    
        }
    
    }