Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #2322: Minimum Score After Removals on a Tree

In this guide, we solve Leetcode #2322 Minimum Score After Removals on a Tree in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

There is an undirected connected tree with n nodes labeled from 0 to n - 1 and n - 1 edges. You are given a 0-indexed integer array nums of length n where nums[i] represents the value of the ith node.

Quick Facts

  • Difficulty: Hard
  • Premium: No
  • Tags: Bit Manipulation, Tree, Depth-First Search, Array

Intuition

We need to explore a structure deeply before backing up, which suits DFS.

DFS keeps local context on the call stack and is easy to implement recursively.

Approach

Define a recursive DFS that carries the necessary state.

Combine child results as the recursion unwinds.

Steps:

  • Define a recursive DFS with state.
  • Visit children and combine results.
  • Return the final aggregation.

Example

Input: nums = [1,5,5,4,11], edges = [[0,1],[1,2],[1,3],[3,4]] Output: 9 Explanation: The diagram above shows a way to make a pair of removals. - The 1st component has nodes [1,3,4] with values [5,4,11]. Its XOR value is 5 ^ 4 ^ 11 = 10. - The 2nd component has node [0] with value [1]. Its XOR value is 1 = 1. - The 3rd component has node [2] with value [5]. Its XOR value is 5 = 5. The score is the difference between the largest and smallest XOR value which is 10 - 1 = 9. It can be shown that no other pair of removals will obtain a smaller score than 9.

Python Solution

class Solution: def minimumScore(self, nums: List[int], edges: List[List[int]]) -> int: def dfs(i: int, fa: int) -> int: res = nums[i] for j in g[i]: if j != fa: res ^= dfs(j, i) return res def dfs2(i: int, fa: int) -> int: nonlocal s, s1, ans res = nums[i] for j in g[i]: if j != fa: s2 = dfs2(j, i) res ^= s2 mx = max(s ^ s1, s2, s1 ^ s2) mn = min(s ^ s1, s2, s1 ^ s2) ans = min(ans, mx - mn) return res g = defaultdict(list) for a, b in edges: g[a].append(b) g[b].append(a) s = reduce(lambda x, y: x ^ y, nums) n = len(nums) ans = inf for i in range(n): for j in g[i]: s1 = dfs(i, j) dfs2(i, j) return ans

Complexity

The time complexity is O(n2)O(n^2)O(n2), and the space complexity is O(n)O(n)O(n), where nnn is the number of nodes in the tree. The space complexity is O(n)O(n)O(n), where nnn is the number of nodes in the tree.

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy