Problem
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. You are also given a 2D integer array edges
of length n - 1
where edges[i] = [ai, bi]
indicates that there is an edge between nodes ai
and bi
in the tree.
Remove two distinct edges of the tree to form three connected components. For a pair of removed edges, the following steps are defined:
- Get the XOR of all the values of the nodes for each of the three components respectively.
- The difference between the largest XOR value and the smallest XOR value is the score of the pair.
- For example, say the three components have the node values:
[4,5,7]
,[1,9]
, and[3,3,3]
. The three XOR values are4 ^ 5 ^ 7 = _**6**_
,1 ^ 9 = _**8**_
, and3 ^ 3 ^ 3 = _**3**_
. The largest XOR value is8
and the smallest XOR value is3
. The score is then8 - 3 = 5
.
Return theminimum score of any possible pair of edge removals on the given tree.
Examples
Example 1
graph TD 1((5)) 0((1)) 2((5)) 3((4)) 4((11)) 0 --- 1 1 --- 2 1 --- 3 3 --- 4
|
|
Example 2
graph TD 0((5)) 1((5)) 2((2)) 3((4)) 4((4)) 5((2)) 0 --- 1 1 --- 2 1 --- 3 3 --- 4 2 --- 5
|
|
Constraints
n == nums.length
3 <= n <= 1000
1 <= nums[i] <= 10^8
edges.length == n - 1
edges[i].length == 2
0 <= ai, bi < n
ai != bi
edges
represents a valid tree.
Solution
Method 1 – DFS Subtree XORs + Edge Removal Simulation
Intuition
For each edge, removing it splits the tree into two subtrees. Removing a second edge in one subtree splits it further. Precompute subtree XORs for all nodes. For each pair of edges, simulate removals and compute the three component XORs.
Approach
- Build tree, run DFS to compute subtree XORs and parent for each node.
- For each edge (u,v), let v be child of u. For each other edge (x,y), let y be child of x.
- For each pair, determine the three components and their XORs using subtree XORs and total XOR.
- Track the minimum score.
Code
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n^2)
— For all pairs of edges. - 🧺 Space complexity:
O(n)
— For tree and subtree XORs.
Method 2 – Single DFS with Entry/Exit Times
Intuition
By using a single DFS traversal, we can record entry and exit times for each node, which allows us to quickly determine ancestor-descendant relationships. We also compute the XOR of each subtree during this traversal. This structure enables us to efficiently enumerate all valid ways to split the tree by removing two edges and calculate the resulting component XORs.
Approach
- Perform a DFS from the root, recording for each node its entry (
in[x]
) and exit (out[x]
) times, and the XOR of its subtree (sum[x]
). - For every pair of non-root nodes
u
andv
, consider removing the edge to their parent for each. - Use the entry/exit times to check if one node is an ancestor of the other, and compute the three component XORs accordingly:
- If
u
is ancestor ofv
: parts aresum[0]⊕sum[u]
,sum[u]⊕sum[v]
,sum[v]
. - If
v
is ancestor ofu
: parts aresum[0]⊕sum[v]
,sum[v]⊕sum[u]
,sum[u]
. - Otherwise: parts are
sum[0]⊕sum[u]⊕sum[v]
,sum[u]
,sum[v]
.
- If
- Track and return the minimum score found.
Code
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n^2)
— For all pairs of non-root nodes. - 🧺 Space complexity:
O(n)
— For tree, entry/exit times, and subtree XORs.