Minimum Subtree Sum Difference After Edge Removal

Quick Overview

Cut exactly one edge of an undirected tree with integer node values and return the smallest absolute difference between the sums of the two components. It tests rooting a tree, computing subtree sums bottom-up with an iterative DFS, and handling negative values in O(n) time.

Minimum Subtree Sum Difference After Edge Removal

Company: Uber

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given an undirected tree with `n` nodes labeled from `0` to `n - 1`. Each node has an associated integer value given in the array `vals`. The tree is described by `edges`, where `edges[i] = [u_i, v_i]` indicates an edge between nodes `u_i` and `v_i`. Remove exactly one edge from the tree to split it into two connected components (subtrees). The sum of a subtree is the sum of all node values in that subtree. Return the minimum possible absolute difference between the sums of the two resulting subtrees. ### Function Signature ```python def minimum_difference(n: int, vals: list[int], edges: list[list[int]]) -> int: ``` ### Rules - `vals[i]` is the value of node `i`, and `len(vals) == n`. - Each edge is undirected. Edges may appear in any order, and either endpoint may be listed first. - The difference for a split is `abs(sum_a - sum_b)`, where `sum_a` and `sum_b` are the sums of the two components. Values can be negative, so a component sum can be negative. ### Constraints - `2 <= n <= 10^4` - `-10^5 <= vals[i] <= 10^5` - `len(edges) == n - 1` - The input forms a valid tree. ### Examples ```text Input: n = 5, vals = [1, 2, 3, 4, 5], edges = [[0, 1], [0, 2], [1, 3], [1, 4]] Output: 5 0(1) / \ 1(2) 2(3) / \ 3(4) 4(5) ``` The total is 15. Removing edge (0,2) gives sums 12 and 3 (difference 9). Removing (0,1) gives 4 and 11 (difference 7), removing (1,3) gives 11 and 4 (difference 7), and removing (1,4) gives 10 and 5 (difference 5), so the minimum is 5. ```text Input: n = 3, vals = [10, 10, 10], edges = [[0, 1], [1, 2]] Output: 10 ``` The tree is the path 0 - 1 - 2 with total 30. Either removal leaves one node with 10 and two nodes with 20, so the difference is 10 both ways. ```text Input: n = 2, vals = [5, -3], edges = [[0, 1]] Output: 8 ``` The only edge splits the tree into sums 5 and -3, and the absolute difference is `abs(5 - (-3)) = 8`.

Overview: Cut exactly one edge of an undirected tree with integer node values and return the smallest absolute difference between the sums of the two components. It tests rooting a tree, computing subtree sums bottom-up with an iterative DFS, and handling negative values in O(n) time.

Uber logo
Uber
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given an undirected tree with n nodes labeled from 0 to n - 1. Each node has an associated integer value given in the array vals. The tree is described by edges, where edges[i] = [u_i, v_i] indicates an edge between nodes u_i and v_i.

Remove exactly one edge from the tree to split it into two connected components (subtrees). The sum of a subtree is the sum of all node values in that subtree.

Return the minimum possible absolute difference between the sums of the two resulting subtrees.

Function Signature

def minimum_difference(n: int, vals: list[int], edges: list[list[int]]) -> int:

Rules

  • vals[i] is the value of node i , and len(vals) == n .
  • Each edge is undirected. Edges may appear in any order, and either endpoint may be listed first.
  • The difference for a split is abs(sum_a - sum_b) , where sum_a and sum_b are the sums of the two components. Values can be negative, so a component sum can be negative.

Constraints

  • 2 <= n <= 10^4
  • -10^5 <= vals[i] <= 10^5
  • len(edges) == n - 1
  • The input forms a valid tree.

Examples

Input: n = 5, vals = [1, 2, 3, 4, 5], edges = [[0, 1], [0, 2], [1, 3], [1, 4]]
Output: 5

        0(1)
       /    \
     1(2)   2(3)
     /  \
   3(4)  4(5)

The total is 15. Removing edge (0,2) gives sums 12 and 3 (difference 9). Removing (0,1) gives 4 and 11 (difference 7), removing (1,3) gives 11 and 4 (difference 7), and removing (1,4) gives 10 and 5 (difference 5), so the minimum is 5.

Input: n = 3, vals = [10, 10, 10], edges = [[0, 1], [1, 2]]
Output: 10

The tree is the path 0 - 1 - 2 with total 30. Either removal leaves one node with 10 and two nodes with 20, so the difference is 10 both ways.

Input: n = 2, vals = [5, -3], edges = [[0, 1]]
Output: 8

The only edge splits the tree into sums 5 and -3, and the absolute difference is abs(5 - (-3)) = 8.