C++ || BFS with explanation || Beginner Friendly
Intuition\nThe problem requires two things to be thought of:\n\nQ1) What could be the sum of each component?\nQ2) How can we check whether the tree could be spl
# Intuition\nThe problem requires two things to be thought of:\n\n*Q1) What could be the sum of each component?\nQ2) How can we check whether the tree could be split into components of given sum?*\n\n1) Add the value of all the nodes and try all the possible sums into which this value can be broken down.\n2) Start travelling from the leaf nodes and move towards the root. A leaf node can be considered only if its value equals the sum. If it\'s value is less than the sum then it must give its to its parent. On the other hand if its value is greater than sum we can conclude that the tree cant be broken into clusters of that sum.\n\n*Please upvote if found helpful :)*\n# Code\n```\nclass Solution {\npublic:\n    bool bfs(int rt, vector<vector<int>> &adj,vector<int>&nums, int sum){\n        stack<vector<int>> st;\n        vector<int> vis(adj.size());\n        queue<int> q;\n        q.push(rt);\n        vector<int> first={rt};\n        st.push(first);\n\n\n        while(!q.empty()){\n            int siz=q.size();\n            vector<int> tem;\n            for(int i=0;i<siz;i++){\n                int nod=q.front();\n                vis[nod]=1;\n                q.pop();\n                for(auto it:adj[nod]){\n                    if(!vis[it]){\n                        q.push(it);\n                        tem.push_back(it);\n                    }\n                }\n            }\n            st.push(tem);\n        }\n        vector<int> pval(adj.size());\n        for(int i =0;i<vis.size();i++){\n            vis[i]=0;\n        }\n        while(!st.empty()){\n            \n            vector<int> tem=st.top();\n            for(int i=0;i<tem.size();i++){\n                vis[tem[i]]=1;\n                if(pval[tem[i]]+nums[tem[i]]>sum){\n                    return 0;\n                }else if(pval[tem[i]]+nums[tem[i]]<sum){\n                    for(auto it: adj[tem[i]]){\n                        if(!vis[it]){\n                            \n                            pval[it]+=pval[tem[i]]+nums[tem[i]];\n                        }\n                    }\n                } \n            }\n            st.pop();\n        }\n        if(pval[0]+nums[0]!=sum) return 0;\n        return 1;\n    }\n    int componentValue(vector<int>& nums, vector<vector<int>>& edges) {\n        int sum=accumulate(nums.begin(),nums.end(),0);\n        vector<vector<int>> adj(nums.size());\n        for(int i=0;i<edges.size();i++){\n            adj[edges[i][0]].push_back(edges[i][1]);\n            adj[edges[i][1]].push_back(edges[i][0]);\n        }\n        int maxi=INT_MIN;\n        for(int i=1;i<=sqrt(sum);i++){\n            if(sum%i==0){\n                if(bfs(0,adj,nums,sum/i)){\n                    maxi=max(maxi,i-1);\n                }\n                int quo=sum/i;\n                if(bfs(0,adj,nums,sum/quo)){\n                    maxi=max(maxi,quo-1);\n                }\n            }\n        }\n        return maxi;\n    }\n};\n```
['Breadth-First Search', 'C++']