Problem solution · Python

Subtree Inversion Sum II

Subtree Inversion Sum II: a Python 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
101 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

  • 101 lines of Python from the credited upstream file subtree-inversion-sum-ii.py.
  • The implementation visibly relies on sequence storage.
  • No explicit 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 · PythonPython
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(object):    def subtreeInversionSum(self, edges, nums, k):        """        :type edges: List[List[int]]        :type nums: List[int]        :type k: int        :rtype: int        """        def iter_dfs():            result = []            stk = [(1, (0, -1, result))]            while stk:                step, args = stk.pop()                if step == 1:                    u, p, ret = args                    ret[:] = [[nums[u]]*k, [nums[u]]*k]                    stk.append((4, (u, p, ret)))                    stk.append((2, (u, p, 0, ret)))                elif step == 2:                    u, p, i, ret = args                    if i == len(adj[u]):                        continue                    v = adj[u][i]                    stk.append((2, (u, p, i+1, ret)))                    if v == p:                        continue                    new_ret = []                    stk.append((3, (new_ret, ret)))                    stk.append((1, (v, u, new_ret)))                elif step == 3:                    new_ret, ret = args                    new_dp1, new_dp2 = new_ret                    dp1, dp2 = ret                    for i in xrange(k//2):                        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 i in xrange(k//2, k):                        dp1[i] += new_dp1[i]                        dp2[i] += new_dp2[i]                    for i in reversed(xrange(k-1)):                        dp1[i] = max(dp1[i], dp1[i+1])                        dp2[i] = min(dp2[i], dp2[i+1])                elif step == 4:                    u, p, ret = args                    dp1, dp2 = ret                    dp1.insert(0, max(dp1[0], -dp2[-1]))                    dp2.insert(0, min(dp2[0], -dp1[-1]))                    dp1.pop()                    dp2.pop()            return result[0][0]         adj = [[] for _ in xrange(len(nums))]        for u, v in edges:            adj[u].append(v)            adj[v].append(u)        return iter_dfs()  # Time:  O(n * k)# Space: O(n + h * k)# dfs, tree dpclass Solution2(object):    def subtreeInversionSum(self, edges, nums, k):        """        :type edges: List[List[int]]        :type nums: List[int]        :type k: int        :rtype: int        """        def dfs(u, p):            dp1, dp2 = [nums[u]]*k, [nums[u]]*k            for v in adj[u]:                if v == p:                    continue                new_dp1, new_dp2 = dfs(v, u)                for i in xrange(k//2):                    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 i in xrange(k//2, k):                    dp1[i] += new_dp1[i]                    dp2[i] += new_dp2[i]                for i in reversed(xrange(k-1)):                    dp1[i] = max(dp1[i], dp1[i+1])                    dp2[i] = min(dp2[i], dp2[i+1])            dp1.insert(0, max(dp1[0], -dp2[-1]))            dp2.insert(0, min(dp2[0], -dp1[-1]))            dp1.pop()            dp2.pop()            return dp1, dp2         adj = [[] for _ in xrange(len(nums))]        for u, v in edges:            adj[u].append(v)            adj[v].append(u)        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 ↗