Factorization || Recursive || C++
class Solution {\nprivate:\n    vector a[20000+5];\n    pair dfs(int curr,int par,int k,vector &nums)\n    {\n        bool ans=true;\n        int sum=nums[curr]
class Solution {\nprivate:\n    vector<int> a[20000+5];\n    pair<int,bool> dfs(int curr,int par,int k,vector<int> &nums)\n    {\n        bool ans=true;\n        int sum=nums[curr];\n        for(auto &it:a[curr])\n        {\n            if(it!=par)\n            {\n                pair<int,bool> pp=dfs(it,curr,k,nums);\n                ans=ans && pp.second;\n                sum+=pp.first;\n            }\n        }\n        \n        if(sum==k) \n        {\n            sum=0;\n            ans=ans and true;\n        }\n        else\n        {\n            if(sum>k) ans=false;\n        }\n        \n        return {sum,ans};\n    }\npublic:\n    int componentValue(vector<int>& nums, vector<vector<int>>& edges) {\n        \n        for(auto &it:edges)\n        {\n            int x=it[0];\n            int y=it[1];\n            a[x].push_back(y);\n            a[y].push_back(x);\n        }\n        \n        int sum=0;\n        for(auto &it:nums) sum+=it;\n        \n        \n        for(int x=1;x<=sum;x++)\n        {\n            if(sum%x==0 and dfs(0,-1,x,nums).second==true)\n            {\n                int cc=sum/x;\n                return (sum/x)-1;\n            }\n        }\n        return 0;\n    }\n};
['Depth-First Search', 'C']