Maximum Genetic Difference Query Problem
Description
LeetCode Problem 1938.
There is a rooted tree consisting of n nodes numbered 0 to n - 1. Each node's number denotes its unique genetic value (i.e. the genetic value of node x is x). The genetic difference between two genetic values is defined as the bitwise-XOR of their values. You are given the integer array parents, where parents[i] is the parent for node i. If node x is the root of the tree, then parents[x] == -1.
You are also given the array queries where queries[i] = [node_i, val_i]. For each query i, find the maximum genetic difference between val_i and p_i, where p_i is the genetic value of any node that is on the path between node_i and the root (including node_i and the root). More formally, you want to maximize val_i XOR p_i.
Return an array ans where ans[i] is the answer to the i^th query.
Example 1:

Input: parents = [-1,0,1,1], queries = [[0,2],[3,2],[2,5]]
Output: [2,3,7]
Explanation: The queries are processed as follows:
- [0,2]: The node with the maximum genetic difference is 0, with a difference of 2 XOR 0 = 2.
- [3,2]: The node with the maximum genetic difference is 1, with a difference of 2 XOR 1 = 3.
- [2,5]: The node with the maximum genetic difference is 2, with a difference of 5 XOR 2 = 7.
Example 2:

Input: parents = [3,7,-1,2,0,7,0,2], queries = [[4,6],[1,15],[0,5]]
Output: [6,14,7]
Explanation: The queries are processed as follows:
- [4,6]: The node with the maximum genetic difference is 0, with a difference of 6 XOR 0 = 6.
- [1,15]: The node with the maximum genetic difference is 1, with a difference of 15 XOR 1 = 14.
- [0,5]: The node with the maximum genetic difference is 2, with a difference of 5 XOR 2 = 7.
Constraints:
2 <= parents.length <= 10^50 <= parents[i] <= parents.length - 1for every nodeithat is not the root.parents[root] == -11 <= queries.length <= 3 * 10^40 <= node_i <= parents.length - 10 <= val_i <= 2 * 10^5
Solution
Approach: Binary Trie on the Root Path
A query (node, val) asks for the maximum of val XOR p over all nodes p on the path from node up to the root. Walking up for every query would be too slow.
Instead, run a depth-first search from the root and keep a binary trie that contains exactly the values on the current root path: insert a node's value (as an 18-bit number) when the search enters it and remove it when the search leaves. Each trie node stores how many inserted values pass through it.
While the search is at node, answer every query attached to it by walking the trie from the top bit down: at each bit, prefer the child whose bit is different from the one in val (when its count is positive), because that makes the XOR bit equal to 1; otherwise follow the same-bit child.
The search is written with an explicit stack, since the tree can be a chain of 100,000 nodes.
Complexity: O((n + q) * log V) time and O(n * log V) space, where n is the number of nodes, q the number of queries and V the largest value (here 18 bits).
class Solution:
def maxGeneticDifference(self, parents: List[int], queries: List[List[int]]) -> List[int]:
n = len(parents)
children = [[] for _ in range(n)]
root = 0
for i, p in enumerate(parents):
if p < 0:
root = i
else:
children[p].append(i)
asked = [[] for _ in range(n)]
for j, (node, val) in enumerate(queries):
asked[node].append(j)
# Binary trie over 18 bits; cnt[v] is how many values on the current root path pass through node v.
child = [0, 0]
cnt = [0]
def update(x, d):
node = 0
for b in range(17, -1, -1):
bit = (x >> b) & 1
nxt = child[node * 2 + bit]
if not nxt:
nxt = len(cnt)
cnt.append(0)
child.extend((0, 0))
child[node * 2 + bit] = nxt
node = nxt
cnt[node] += d
def query(v):
node = 0
res = 0
for b in range(17, -1, -1):
bit = (v >> b) & 1
nxt = child[node * 2 + (bit ^ 1)]
if nxt and cnt[nxt] > 0:
res |= 1 << b
node = nxt
else:
node = child[node * 2 + bit]
return res
ans = [0] * len(queries)
stack = [root]
while stack:
x = stack.pop()
if x >= 0:
update(x, 1)
for j in asked[x]:
ans[j] = query(queries[j][1])
stack.append(~x)
stack.extend(children[x])
else:
update(~x, -1)
return ans