✅C++|| DFS || Easiest and Simplest Solution
\n# Approach\nIdea is to keep the sum of subtree of each node, and store them in an array, say s. Now for all the factors fac of A (A is the sum of values of al
\n# Approach\nIdea is to keep the sum of subtree of each node, and store them in an array, say `s`. Now for all the factors `fac` of `A` (`A` is the sum of values of all the nodes.), check if there are exactly ` A / fac ` elemens in s that are divisible by `fac`.\n\n# Complexity\n- Time complexity: O(n log(A))\nA is the sum of values of all the nodes.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n\n# Code\n```\nclass Solution {\npublic:\n\n    int dfs(int node, vector<int> &s, vector<int> &nums, vector<vector<int>> &mp, vector<int> &vis)\n    {\n        vis[node] = 1;\n        for(auto x: mp[node])\n            if(vis[x]==0)\n                s[node]+=dfs(x, s, nums, mp, vis);\n        s[node]+=nums[node];\n        return s[node];\n    }\n\n    int componentValue(vector<int>& nums, vector<vector<int>>& edges) {\n        vector<vector<int>> mp(nums.size());\n        for(auto x: edges){\n            mp[x[0]].push_back(x[1]);\n            mp[x[1]].push_back(x[0]);\n        }\n        int sum = 0;\n        vector<int> s(nums.size(), 0), vis(nums.size(), 0);\n        sum = dfs(0, s, nums, mp, vis);\n        for(int i=1; i<sum; i++)\n        {\n            if(sum%i!=0)\n            {\n                int div=0;\n                for(auto x: s)\n                    if(x%i==0)\n                        x++;\n                if(x==sum/i)\n                    return sum/i-1;\n            }\n        }\n        return 0;\n    }\n};\n```
['Depth-First Search', 'C++']