Python | DFS | Easy to Understand
\nclass Solution:\n    def getFactors(self, num: int) -> List[int]:\n        factors = []\n        for possibleFactor in range(1, int(sqrt(num)) + 1):\n        
```\nclass Solution:\n    def getFactors(self, num: int) -> List[int]:\n        factors = []\n        for possibleFactor in range(1, int(sqrt(num)) + 1):\n            if num % possibleFactor != 0: \n                continue\n            factors.append(possibleFactor)\n            if num // possibleFactor != possibleFactor: \n                factors.append(num // possibleFactor)\n        return factors\n    \n    def componentValue(self, nums: List[int], edges: List[List[int]]) -> int:\n        numNodes, self.cntRemainderZero, graph = len(nums), 0, defaultdict(list)\n        for node1, node2 in edges:\n            graph[node1].append(node2)\n            graph[node2].append(node1)\n        \n        def dfs(node: int, prevNode: int, sumPerComponent: int) -> int:\n            remainder = nums[node]\n            for neighbor in graph[node]:\n                if neighbor == prevNode:\n                    continue\n                remainder += dfs(neighbor, node, sumPerComponent)\n            remainder %= sumPerComponent\n            if remainder == 0:\n                self.cntRemainderZero += 1\n            return remainder\n        \n        def isGood(sumPerComponent: int, expectedNumOfComponents: int) -> bool:\n            self.cntRemainderZero = 0\n            dfs(0, -1, sumPerComponent)\n            return self.cntRemainderZero == expectedNumOfComponents\n        \n        sumOfAllNodes, maxNode = sum(nums), max(nums)\n        for sumPerComponent in sorted(self.getFactors(sumOfAllNodes)):\n            if sumPerComponent < maxNode: \n                continue\n            expectedNumOfComponents = sumOfAllNodes // sumPerComponent\n            if isGood(sumPerComponent, expectedNumOfComponents):\n                return expectedNumOfComponents - 1\n        return 0\n```
['Depth-First Search', 'Recursion', 'Python']