Java | DFS
\n\t//1.DFS\n    //Runtime: 64 ms, faster than 97.17% of Java online submissions for Create Components With Same Value.\n    //Memory Usage: 103.1 MB, less than
```\n\t//1.DFS\n    //Runtime: 64 ms, faster than 97.17% of Java online submissions for Create Components With Same Value.\n    //Memory Usage: 103.1 MB, less than 83.02% of Java online submissions for Create Components With Same Value.\n    //Time: O(V + V + E + logV * V); Space: O(V + E + V)\n    //Time: O(logV * V); Space: O(V)\n    public int componentValue(int[] nums, int[][] edges) {\n        if (edges.length == 0) return 0;\n\n        //Time: O(V)\n        int sum = 0, maxValue = 0;\n        for (int i = 0; i < nums.length; i++){\n            sum += nums[i];\n            maxValue = Math.max(maxValue, nums[i]);\n        }\n        int maxCount = sum / maxValue;\n        if (sum % maxCount == 0 && maxCount == nums.length) return nums.length - 1;\n\n        //build graph\n        //Time: O(V + E); Space: O(V + E)\n        List<Integer>[] graph = new List[nums.length];\n        for (int i = 0; i < nums.length; i++) graph[i] = new ArrayList<>();\n        for (int[] edge: edges){\n            graph[edge[0]].add(edge[1]);\n            graph[edge[1]].add(edge[0]);\n        }\n\n        //Time: O(logV * V); Space:O(V)\n        for (int k = maxCount; k > 1; k--)\n            if (sum % k == 0 && helper_dfs_sumTarget(nums, graph,0, -1, sum / k) == 0)\n                return k - 1;\n        return 0;\n    }\n\n    private int helper_dfs_sumTarget(int[] nums, List<Integer>[] graph,\n                       int index, int parent, int target) {\n        //the sum of value of current subtree\n        int sum = nums[index];\n        for (int neighbour : graph[index]){\n            if (neighbour != parent) {\n                sum += helper_dfs_sumTarget(nums, graph, neighbour, index, target);\n                if (sum > target) return sum;\n            }\n        }\n        //sum == target means current subtree can be deleted from tree.\n        return sum == target ? 0 : sum;\n    }\n```
['Depth-First Search', 'Java']