Subtree Inversion Sum - Problem

You are given an undirected tree rooted at node 0, with n nodes numbered from 0 to n-1. The tree is represented by a 2D integer array edges where edges[i] = [ui, vi] indicates an edge between nodes ui and vi.

You are also given:

  • An integer array nums of length n, where nums[i] represents the value at node i
  • An integer k representing the minimum distance constraint

Subtree Inversion Operation: When you invert a node, every value in the subtree rooted at that node is multiplied by -1.

Distance Constraint: You may only invert a node if it is "sufficiently far" from any other inverted node. Specifically, if you invert two nodes a and b such that one is an ancestor of the other, then the distance between them must be at least k.

Your goal is to maximize the sum of all node values in the tree after applying optimal inversion operations.

Input & Output

example_1.py — Basic Tree
$ Input: edges = [[0,1],[0,2]], nums = [1,-3,4], k = 2
Output: 8
💡 Note: We can invert node 1 (which only affects itself since it's a leaf). This changes nums[1] from -3 to 3. The final sum is 1 + 3 + 4 = 8.
example_2.py — Distance Constraint
$ Input: edges = [[0,1],[1,2],[1,3]], nums = [1,-2,-3,4], k = 2
Output: 2
💡 Note: We can invert node 1, which changes the subtree rooted at node 1. Original values [-2,-3,4] in the subtree become [2,3,-4]. The final tree values are [1,2,3,-4] with sum = 1 + 2 + 3 + (-4) = 2.
example_3.py — Single Node
$ Input: edges = [], nums = [-5], k = 1
Output: 5
💡 Note: With only one node, we can invert it to change -5 to 5, giving us the maximum possible sum of 5.

Constraints

  • 1 ≤ n ≤ 20
  • edges.length = n - 1
  • 0 ≤ edges[i][0], edges[i][1] < n
  • The input represents a valid tree
  • -104 ≤ nums[i] ≤ 104
  • 1 ≤ k ≤ n
  • The tree is connected and acyclic

Visualization

Tap to expand
Subtree Inversion Sum INPUT Tree Structure (rooted at 0) 0 val=1 1 val=-3 2 val=4 edges = [[0,1],[0,2]] nums = [1, -3, 4] k = 2 (min distance) Initial sum: 1+(-3)+4 = 2 Goal: Maximize sum via inversions ALGORITHM (DFS) 1 Root DFS Traversal Start from node 0, visit all subtrees recursively 2 Track Inversion State For each node, track if inverted & distance to ancestor 3 Check Distance k Only invert if dist to nearest inverted ancestor >= k 4 Choose Optimal At each node: compare sum with/without inversion Optimal Decision: Invert node 1: -3 --> 3 Keep node 0: 1 (no change) Keep node 2: 4 (no change) FINAL RESULT After Optimal Inversions 0 1 1 3 (inverted) 2 4 Sum Calculation: 1 + 3 + 4 = 8 Output: 8 Key Insight: DFS with memoization tracks optimal inversion choices. At each node, we compute max sum considering: 1) Distance constraint k between inverted ancestors, 2) Whether inverting subtree improves total sum. Inverting node 1 changes -3 to 3 (gain of 6), maximizing final sum to 8. Distance from root = 1 < k=2, so valid. TutorialsPoint - Subtree Inversion Sum | DFS Approach
Asked in
Google 15 Meta 12 Amazon 8 Microsoft 6
28.5K Views
Medium Frequency
~35 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen