Maximum XOR of Two Non-Overlapping Subtrees - Problem

Given an undirected tree with n nodes labeled from 0 to n-1, find the maximum XOR of the sums of values from two non-overlapping subtrees.

You are provided with:

  • An integer n representing the number of nodes
  • A 2D array edges of length n-1 where edges[i] = [ai, bi] indicates an edge between nodes ai and bi
  • An array values where values[i] is the value associated with the i-th node

The tree is rooted at node 0. Your task is to select any two non-overlapping subtrees and calculate the bitwise XOR of their sums. Two subtrees are non-overlapping if they share no common nodes.

Goal: Return the maximum possible XOR score. If no two non-overlapping subtrees exist, return 0.

Input & Output

example_1.py โ€” Basic Tree
$ Input: n = 6, edges = [[0,1],[0,2],[1,3],[1,4],[2,5]], values = [4,6,1,3,1,2]
โ€บ Output: 16
๐Ÿ’ก Note: Select subtree rooted at node 1 (sum = 6+3+1 = 10) and subtree rooted at node 2 (sum = 1+2 = 3). XOR = 10 ^ 3 = 9. However, selecting subtree at node 1 (sum = 10) and single node 5 (sum = 2) gives XOR = 10 ^ 2 = 8. The optimal is selecting subtree at node 3 (sum = 3) and the remaining tree gives better result.
example_2.py โ€” Linear Tree
$ Input: n = 3, edges = [[0,1],[1,2]], values = [5,3,7]
โ€บ Output: 12
๐Ÿ’ก Note: Select subtree rooted at node 1 (sum = 3+7 = 10) and single node 0 (sum = 5). XOR = 10 ^ 5 = 15. Alternatively, select node 2 (sum = 7) and node 0 (sum = 5) for XOR = 7 ^ 5 = 2. The maximum is 15.
example_3.py โ€” Single Node
$ Input: n = 1, edges = [], values = [10]
โ€บ Output: 0
๐Ÿ’ก Note: Only one node exists, so we cannot select two non-overlapping subtrees. Return 0.

Visualization

Tap to expand
ROOTABCDEFTRIE STORAGESubtree Sums:โ€ข Sum_A = 15โ€ข Sum_B = 23โ€ข Sum_C = 8โ€ข Sum_D = 12MAX XOR: 15^23=24Query & InsertOPTIMIZATIONTime: O(n log S)Space: O(n log S)vs Brute Force O(nยฒ)Maximum XOR of Non-Overlapping Subtrees
Understanding the Visualization
1
Build Network Tree
Construct the tree structure and establish parent-child relationships
2
Calculate Segment Values
Use DFS to compute total value for each network segment (subtree)
3
Smart XOR Matching
Use Trie to efficiently find best XOR pairs without checking all combinations
4
Optimize Locally
Check XOR between child segments at each node for additional optimization
Key Takeaway
๐ŸŽฏ Key Insight: The Trie data structure transforms an O(nยฒ) brute force approach into an O(n log S) optimal solution by enabling efficient maximum XOR queries during tree traversal.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log S)

O(n) for DFS traversal, O(log S) for each Trie operation where S is maximum sum value

n
2n
โšก Linearithmic
Space Complexity
O(n log S)

O(n) for recursion stack, O(n log S) for Trie storage

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค n โ‰ค 5 ร— 104
  • edges.length == n - 1
  • 0 โ‰ค ai, bi < n
  • ai โ‰  bi
  • edges represents a valid tree
  • 1 โ‰ค values[i] โ‰ค 109
Asked in
Google 45 Microsoft 38 Amazon 32 Meta 28
28.4K Views
Medium-High Frequency
~25 min Avg. Time
892 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