Problem solution · Python

Maximum XOR of Two Non-Overlapping Subtrees

Maximum XOR of Two Non-Overlapping Subtrees: a Python solution using depth-first search. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Depth-first search
Source
walkccc LeetCode Solutions
Length
68 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Depth-first search

For Maximum XOR of Two Non-Overlapping Subtrees, the implementation follows one branch at a time, making it suitable for components, trees, backtracking, or dependency exploration.

  1. Define the state carried into one recursive or stack frame.
  2. Mark or choose the current state before exploring children.
  3. Combine child results or undo the choice when the branch finishes.

Code notes

  • 68 lines of Python from the credited upstream file 2479.py.
  • The implementation visibly relies on sequence storage.
  • No explicit loop blocks detected, together with recursive traversal.

Complexity

Count unique states for graph traversal; for backtracking, count the branching factor and maximum depth.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeMaximum XOR of Two Non-Overlapping Subtrees · PythonPython
Use this to learn the idea, then write your own version.
class TrieNode:  def __init__(self):    self.children: list[TrieNode | None] = [None] * 2  class BitTrie:  def __init__(self, maxBit: int):    self.maxBit = maxBit    self.root = TrieNode()   def insert(self, num: int) -> None:    node = self.root    for i in range(self.maxBit, -1, -1):      bit = num >> i & 1      if not node.children[bit]:        node.children[bit] = TrieNode()      node = node.children[bit]   def getMaxXor(self, num: int) -> int:    maxXor = 0    node = self.root    for i in range(self.maxBit, -1, -1):      bit = num >> i & 1      toggleBit = bit ^ 1      if node.children[toggleBit]:        maxXor = maxXor | 1 << i        node = node.children[toggleBit]      elif node.children[bit]:        node = node.children[bit]      else:  # There's nothing in the Bit Trie.        return 0    return maxXor  class Solution:  def maxXor(self, n: int, edges: list[list[int]], values: list[int]) -> int:    ans = 0    tree = [[] for _ in range(n)]    treeSums = [0] * n     for u, v in edges:      tree[u].append(v)      tree[v].append(u)     # Gets the tree sum rooted at node u.    def getTreeSum(u: int, prev: int) -> int:      treeSum = values[u] + sum(getTreeSum(v, u) for v in tree[u] if v != prev)      treeSums[u] = treeSum      return treeSum     def dfs(u: int, prev: int, bitTrie: BitTrie) -> None:      nonlocal ans      for v in tree[u]:        if v == prev:          continue        # Preorder to get the ans.        ans = max(ans, bitTrie.getMaxXor(treeSums[v]))        # Recursively call on the subtree rooted at node v.        dfs(v, u, bitTrie)        # Postorder to insert the tree sum rooted at node v.        bitTrie.insert(treeSums[v])     getTreeSum(0, -1)    maxBit = int(math.log2(max(treeSums[1:])))    # Similar to 421. Maximum XOR of Two Numbers in an Array    dfs(0, -1, BitTrie(maxBit))    return ans 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗