Problem solution · C++

Subtree Inversion Sum II

Subtree Inversion Sum II: a C++ solution using depth-first search. Learn the idea, check the complexity, and read the full code, with credit to Kamyu LeetCode Solutions.

Technique
Depth-first search
Source
Kamyu LeetCode Solutions
Length
107 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Depth-first search

For Subtree Inversion Sum II, the implementation follows one branch at a time, making it suitable for components, trees, backtracking, or dependency exploration.

  1. Define the state carried into one recursive or stack frame.
  2. Mark or choose the current state before exploring children.
  3. Combine child results or undo the choice when the branch finishes.

Code notes

  • 107 lines of C++ from the credited upstream file subtree-inversion-sum-ii.cpp.
  • The implementation visibly relies on sequence storage.
  • 10 loop blocks detected, together with recursive traversal.

Complexity

Count unique states for graph traversal; for backtracking, count the branching factor and maximum depth.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from Kamyu LeetCode Solutions by kamyu104 and is used under the MIT licence.

Full codeSubtree Inversion Sum II · C++C++
Use this to learn the idea, then write your own version.
// Time:  O(n * k)// Space: O(n + h * k) // iterative dfs, tree dpclass Solution {public:    int subtreeInversionSum(vector<vector<int>>& edges, vector<int>& nums, int k) {        vector<vector<int>> adj(size(nums));        const auto& iter_dfs = [&]() {            using RET = vector<vector<int64_t>>;            RET result{};            vector<tuple<int, int, int, int, shared_ptr<RET>, RET *>> stk = {{1, 0, -1, -1, nullptr, &result}};            while (!empty(stk)) {                const auto [step, u, p, i, new_ret, ret] = stk.back(); stk.pop_back();                if (step == 1) {                    ret->assign(2, vector<int64_t>(k, nums[u]));                    stk.emplace_back(4, -1, -1, -1, nullptr, ret);                    stk.emplace_back(2, u, p, 0, nullptr, ret);                } else if (step == 2) {                    if (i == size(adj[u])) {                        continue;                    }                    const auto& v = adj[u][i];                    stk.emplace_back(2, u, p, i + 1, nullptr, ret);                    if (v == p) {                        continue;                    }                    const auto& new_ret = make_shared<RET>();                    stk.emplace_back(3, -1, -1, -1, new_ret, ret);                    stk.emplace_back(1, v, u, -1, nullptr, new_ret.get());                } else if (step == 3) {                    auto& new_dp1 = (*new_ret)[0], &new_dp2 = (*new_ret)[1];                    auto& dp1 = (*ret)[0], &dp2 = (*ret)[1];                    for (int i = 0; i < k / 2; ++i) {                        dp1[i] = max(dp1[i] + new_dp1[(k - 2) - i], dp1[(k - 2) - i] + new_dp1[i]);                        dp2[i] = min(dp2[i] + new_dp2[(k - 2) - i], dp2[(k - 2) - i] + new_dp2[i]);                    }                    for (int i = k / 2; i < k; ++i) {                        dp1[i] += new_dp1[i];                        dp2[i] += new_dp2[i];                    }                    for (int i = k - 2; i >= 0; --i) {                        dp1[i] = max(dp1[i], dp1[i + 1]);                        dp2[i] = min(dp2[i], dp2[i + 1]);                    }                } else if (step == 4) {                    auto& dp1 = (*ret)[0], &dp2 = (*ret)[1];                    const auto mx = max(dp1[0], -dp2[k - 1]);                    const auto mn = min(dp2[0], -dp1[k - 1]);                    dp1.insert(begin(dp1), mx); dp1.pop_back();                    dp2.insert(begin(dp2), mn); dp2.pop_back();                }            }            return result[0][0];        };                for (auto& e : edges) {            adj[e[0]].emplace_back(e[1]);            adj[e[1]].emplace_back(e[0]);        }        return iter_dfs();    }}; // Time:  O(n * k)// Space: O(n + h * k)// dfs, tree dpclass Solution2 {public:    int subtreeInversionSum(vector<vector<int>>& edges, vector<int>& nums, int k) {        vector<vector<int>> adj(size(nums));        const auto dfs = [&](this auto&& dfs, int u, int p) -> pair<vector<int64_t>, vector<int64_t>> {            vector<int64_t> dp1(k, nums[u]), dp2(k, nums[u]);            for (const auto& v : adj[u]) {                if (v == p) {                    continue;                }                const auto& [new_dp1, new_dp2] = dfs(v, u);                for (int i = 0; i < k / 2; ++i) {                    dp1[i] = max(dp1[i] + new_dp1[(k - 2) - i], dp1[(k - 2) - i] + new_dp1[i]);                    dp2[i] = min(dp2[i] + new_dp2[(k - 2) - i], dp2[(k - 2) - i] + new_dp2[i]);                }                for (int i = k / 2; i < k; ++i) {                    dp1[i] += new_dp1[i];                    dp2[i] += new_dp2[i];                }                for (int i = k - 2; i >= 0; --i) {                    dp1[i] = max(dp1[i], dp1[i + 1]);                    dp2[i] = min(dp2[i], dp2[i + 1]);                }            }            const auto mx = max(dp1[0], -dp2[k - 1]);            const auto mn = min(dp2[0], -dp1[k - 1]);            dp1.insert(begin(dp1), mx); dp1.pop_back();            dp2.insert(begin(dp2), mn); dp2.pop_back();            return pair(dp1, dp2);        };                for (auto& e : edges) {            adj[e[0]].emplace_back(e[1]);            adj[e[1]].emplace_back(e[0]);        }        const auto& [dp1, _] = dfs(0, -1);        return dp1[0];    }}; 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗