Problem solution · Java

Maximum XOR of Two Non-Overlapping Subtrees

Maximum XOR of Two Non-Overlapping Subtrees: a Java 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
97 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

  • 97 lines of Java from the credited upstream file 2479.java.
  • The implementation visibly relies on sequence storage.
  • 7 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 · JavaJava
Use this to learn the idea, then write your own version.
class TrieNode {  public TrieNode[] children = new TrieNode[2];} class BitTrie {  public BitTrie(int maxBit) {    this.maxBit = maxBit;  }   public void insert(long num) {    TrieNode node = root;    for (int i = maxBit; i >= 0; --i) {      final int bit = (int) (num >> i & 1);      if (node.children[bit] == null)        node.children[bit] = new TrieNode();      node = node.children[bit];    }  }   public long getMaxXor(long num) {    long maxXor = 0;    TrieNode node = root;    for (int i = maxBit; i >= 0; --i) {      final int bit = (int) (num >> i & 1);      final int toggleBit = bit ^ 1;      if (node.children[toggleBit] != null) {        maxXor = maxXor | 1L << i;        node = node.children[toggleBit];      } else if (node.children[bit] != null) {        node = node.children[bit];      } else { // There's nothing in the Bit Trie.        return 0;      }    }    return maxXor;  }   private int maxBit;  private TrieNode root = new TrieNode();} class Solution {  public long maxXor(int n, int[][] edges, int[] values) {    List<Integer>[] tree = new List[n];    long[] treeSums = new long[n];     for (int i = 0; i < n; ++i)      tree[i] = new ArrayList<>();     for (int[] edge : edges) {      final int u = edge[0];      final int v = edge[1];      tree[u].add(v);      tree[v].add(u);    }     getTreeSum(tree, 0, -1, treeSums, values);    final long maxSubTreeSum = getMaxSubTreeSum(treeSums);    final int maxBit = (int) (Math.log(maxSubTreeSum) / Math.log(2));    // Similar to 421. Maximum XOR of Two Numbers in an Array    dfs(tree, 0, -1, treeSums, new BitTrie(maxBit));    return ans;  }   private long ans = 0;   // Gets the tree sum rooted at node u.  private long getTreeSum(List<Integer>[] tree, int u, int prev, long[] treeSums, int[] values) {    long treeSum = values[u];    for (final int v : tree[u])      if (v != prev)        treeSum += getTreeSum(tree, v, u, treeSums, values);    treeSums[u] = treeSum;    return treeSum;  }   private long getMaxSubTreeSum(long[] treeSums) {    long maxSubTreeSum = 0;    for (int i = 1; i < treeSums.length; ++i)      maxSubTreeSum = Math.max(maxSubTreeSum, treeSums[i]);    return maxSubTreeSum;  }   private void dfs(List<Integer>[] tree, int u, int prev, long[] treeSums, BitTrie bitTrie) {    for (final int v : tree[u]) {      if (v == prev)        continue;      // Preorder to get the ans.      ans = Math.max(ans, bitTrie.getMaxXor(treeSums[v]));      // Recursively call on the subtree rooted at node v.      dfs(tree, v, u, treeSums, bitTrie);      // Postorder to insert the tree sum rooted at node v.      bitTrie.insert(treeSums[v]);    }  }} 

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 ↗