package leetcode;

import java.util.Arrays;
import java.util.List;

/**
 * https://leetcode.com/problems/minimum-time-to-collect-all-apples-in-a-tree/
 */
public class Problem1443 {
    public int minTime(int n, int[][] edges, List<Boolean> hasApple) {
        // TODO
        return 0;
    }

    public static void main(String[] args) {
        Problem1443 prob = new Problem1443();
        System.out.println(prob.minTime(
            7,
            new int[][]{{0,1},{0,2},{1,4},{1,5},{2,3},{2,6}},
            Arrays.asList(false,false,true,false,true,true,false))); // 8
        System.out.println(prob.minTime(
            7,
            new int[][]{{0,1},{0,2},{1,4},{1,5},{2,3},{2,6}},
            Arrays.asList(false,false,true,false,false,true,false))); // 6
        System.out.println(prob.minTime(
            7,
            new int[][]{{0,1},{0,2},{1,4},{1,5},{2,3},{2,6}},
            Arrays.asList(false,false,false,false,false,false,false))); // 0
    }
}

package leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * https://leetcode.com/problems/minimum-time-to-collect-all-apples-in-a-tree/
 */
public class Problem1443 {
    public int minTime(int n, int[][] edges, List<Boolean> hasApple) {
        List<Integer>[] tree = new List[n];
        for (int i = 0; i < tree.length; i++) {
            tree[i] = new ArrayList<>();
        }
        for (int[] edge : edges) {
            int from = edge[0];
            int to = edge[1];
            tree[from].add(to);
        }
        IntRef answer = new IntRef();
        minTime(tree, hasApple, 0, answer);
        return Math.max(0, answer.value - 1);
    }

    private static class IntRef {
        private int value;
    }

    private static boolean minTime(List<Integer>[] tree,
                                   List<Boolean> hasApple,
                                   int node,
                                   IntRef time) {
        boolean found = hasApple.get(node);
        for (int children : tree[node]) {
            time.value++;
            found |= minTime(tree, hasApple, children, time);
        }
        if (found) {
            time.value++;
        } else {
            time.value = Math.max(0, time.value - 1);
        }
        return found;
    }
}

Solve problem 1443