Maximum XOR of Two Non-Overlapping Subtrees Problem
Description
LeetCode Problem 2479.
There is an undirected tree with n nodes labeled from 0 to n - 1. You are given the integer n and a 2D integer array edges of length n - 1, where edges[i] = [a_i, b_i] indicates that there is an edge between nodes a_i and b_i in the tree. The root of the tree is the node labeled 0.
Each node has an associated value. You are given an array values of length n, where values[i] is the value of the i^th node.
Select any two non-overlapping subtrees. Your score is the bitwise XOR of the sum of the values within those subtrees.
Return the maximum possible score you can achieve. If it is impossible to find two nonoverlapping subtrees, return 0.
Note that:
- The subtree of a node is the tree consisting of that node and all of its descendants.
- Two subtrees are non-overlapping if they do not share any common node.
Example 1:

Input: n = 6, edges = [[0,1],[0,2],[1,3],[1,4],[2,5]], values = [2,8,3,6,2,5]
Output: 24
Explanation: Node 1's subtree has sum of values 16, while node 2's subtree has sum of values 8, so choosing these nodes will yield a score of 16 XOR 8 = 24. It can be proved that is the maximum possible score we can obtain.
Example 2:

Input: n = 3, edges = [[0,1],[1,2]], values = [4,6,1]
Output: 0
Explanation: There is no possible way to select two non-overlapping subtrees, so we just return 0.
Constraints:
2 <= n <= 5 * 10^4edges.length == n - 10 <= a_i, b_i < nvalues.length == n1 <= values[i] <= 10^9- It is guaranteed that
edgesrepresents a valid tree.
Solution
Approach: Subtree Sums + Binary Trie
First compute the sum of every subtree with a DFS.
Then run a second DFS in which each node first queries and afterwards inserts its subtree sum into a binary trie. A node queries before visiting its children and inserts after them, so the trie only holds subtrees that are already finished: those of earlier siblings and their relatives, never an ancestor or a descendant of the current node. Those subtrees do not overlap with the current one.
A query walks the trie from the highest bit down, preferring the opposite bit at each step to maximise the XOR. Sums go up to about 5 * 10^13, so 47 bits are enough. The answer is the largest result over all queries, or 0 if there were none.
Complexity: O(n log S) time and O(n log S) space, where n is the number of nodes and S the largest subtree sum (log S is about 47).
from collections import defaultdict
class Trie:
def __init__(self):
self.children = [None] * 2
def insert(self, x):
node = self
for i in range(47, -1, -1):
v = (x >> i) & 1
if node.children[v] is None:
node.children[v] = Trie()
node = node.children[v]
def search(self, x):
node = self
res = 0
for i in range(47, -1, -1):
v = (x >> i) & 1
if node is None:
return res
if node.children[v ^ 1]:
res = res << 1 | 1
node = node.children[v ^ 1]
else:
res <<= 1
node = node.children[v]
return res
class Solution:
def maxXor(self, n: int, edges: List[List[int]], values: List[int]) -> int:
def dfs1(i, fa):
t = values[i]
for j in g[i]:
if j != fa:
t += dfs1(j, i)
s[i] = t
return t
def dfs2(i, fa):
nonlocal ans
ans = max(ans, tree.search(s[i]))
for j in g[i]:
if j != fa:
dfs2(j, i)
tree.insert(s[i])
g = defaultdict(list)
for a, b in edges:
g[a].append(b)
g[b].append(a)
s = [0] * n
dfs1(0, -1)
ans = 0
tree = Trie()
dfs2(0, -1)
return ans