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: 10
๐Ÿ’ก Note: We can invert nodes 0 and 2 since the distance between them is 2 (exactly k). Inverting 0 changes the entire tree to [-1,2,3,-4], then inverting 2 changes it to [-1,2,-3,-4]. Sum = -1 + 2 + (-3) + (-4) = -6. Better: invert only node 1, giving [1,2,3,-4] with sum = 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 StrategyOriginal Tree0val: 21val: -52val: 3After Inverting Node 10val: 21val: +52val: 3Sum: 2 + 5 + 3 = 10Distance Constraint CheckingRules for Valid Inversions:โ€ข If nodes A and B are ancestor-descendantโ€ข AND we want to invert both A and Bโ€ข THEN distance(A, B) must be โ‰ฅ kโ€ข Otherwise the inversion combination is invalid๐Ÿ’ก Key Insight:The optimal approach uses dynamic programming to decide at each node:"Should I invert this subtree, or allow my descendants to make their own inversion decisions?"
Understanding the Visualization
1
Build Tree Structure
Convert the edge list into a rooted tree with parent-child relationships
2
Calculate Distances
For any two nodes, find their distance using LCA (Lowest Common Ancestor)
3
Try All Valid Combinations
Generate all possible inversion sets that satisfy the distance constraint
4
Apply Inversions and Sum
For each valid combination, invert the subtrees and calculate the total sum
5
Return Maximum
Track and return the maximum sum achieved across all valid combinations
Key Takeaway
๐ŸŽฏ Key Insight: At each node, we face a choice - invert the current subtree (affecting all descendants) or allow deeper nodes to make independent inversion decisions. The distance constraint k prevents us from having inversions too close together in the ancestor-descendant chain.
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