#3544

Subtree Inversion Sum

master · 1600 · lc hard +32 · 44.2% accepted · 46 likes · top 26%

Description

An undirected tree rooted at node 0 has node values in nums and integer parameter k. You may invert any subset of nodes: inverting a node multiplies every value in its subtree by -1.

Constraint: if two inverted nodes a and b are in an ancestor-descendant relationship, their path distance must be at least k.

Return the maximum possible sum of all node values after optimal inversions.

Code

1
2
3