BFS
If we split the tree into i components, the target value of each component is sum / i. \n\nNote that we cannot split into i components if sum % i != 0. \n\nFor 
If we split the tree into `i` components, the target value of each component is `sum / i`. \n\nNote that we cannot split into `i` components if `sum % i != 0`. \n\nFor each target value, we can unambiguously discover all components starting from leaves. This works because we have a tree, not a graph. \n\nWhen processing from leaves, we track the value of the current component:\n- If the value is greater than the target, we cannot split the tree.\n- If the value is less, we add it to the parent.\n\t- There is no more than one parent as we go from a leaf.\n- If the value is equal, we do not add it to the parent.\n\t- It will start a new component from that parent.\n\nThus, the solution is to try to `sum / n, sum / (n - 1), ..., sum / 2` values, and use BFS to check if we can do the split.\n\n**Complexity Analysis**\n- Time: O(n * N nm), where m is max(nums), and N is a function returning the number of divisors.\n\t- BFS is O(n), and we do it for each divisor of sum. \n\t- The maximum sum is `nm`, and N(nm) is the number of divisors. \n\t- In the average case, the number of divisorts/target values that won\'t be fast-pruned in BFS can be esitmated as `log2 nm`.\n- Memory: O(n)\n\n**C++**\n`cnt[i]` is the number of edges for node `i`. We start from leaves - nodes with one edge.\n\nAs we process a neighboring node, we decrease its number of edges. When that number becomes `1` - this node becomes a \u201Cleaf\u201D and we add it to the BFS queue.\n\nNote that it also prevents a node from being processed twice - since next time `cnt[i]` would become zero.\n```cpp\nbool bfs(int val, vector<vector<int>> &al, vector<int> q, vector<int> n, vector<int> cnt) {\n    while(!q.empty()) {\n        vector<int> q1;\n        for (auto i : q) {\n            if (n[i] > val)\n                return false;\n            for (auto j : al[i]) {\n                n[j] += n[i] < val ? n[i] : 0;\n                if (--cnt[j] == 1)\n                    q1.push_back(j);\n            }\n        }\n        swap(q, q1);\n    }\n    return true;\n}\nint componentValue(vector<int>& nums, vector<vector<int>>& edges) {\n    int n = nums.size(), sum = accumulate(begin(nums), end(nums), 0);\n    if (sum % n == 0 && all_of(begin(nums), end(nums), [&](int v){ return v == sum / n; }))\n        return n - 1;\n    vector<vector<int>> al(n);\n    for (auto &e : edges) {\n        al[e[0]].push_back(e[1]);\n        al[e[1]].push_back(e[0]);\n    }\n    vector<int> q, cnt(n);\n    for (int i = 0; i < n; ++i) {\n        cnt[i] = al[i].size();\n        if (al[i].size() == 1)\n            q.push_back(i);\n    }\n    for (int i = n - 1; i > 1; --i)\n        if (sum % i == 0 && bfs(sum / i, al, q, nums, cnt))\n            return i - 1;\n    return 0;\n}\n```
['C']