Problem solution · C++

Maximum XOR of Two Non-Overlapping Subtrees

Maximum XOR of Two Non-Overlapping Subtrees: a C++ 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
92 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

  • 92 lines of C++ from the credited upstream file 2479.cpp.
  • The implementation visibly relies on sequence storage.
  • 5 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 · C++C++
Use this to learn the idea, then write your own version.
struct TrieNode {  vector<shared_ptr<TrieNode>> children;  TrieNode() : children(2) {}}; class BitTrie { public:  BitTrie(int maxBit) : maxBit(maxBit) {}   void insert(long num) {    shared_ptr<TrieNode> node = root;    for (int i = maxBit; i >= 0; --i) {      const int bit = num >> i & 1;      if (node->children[bit] == nullptr)        node->children[bit] = make_shared<TrieNode>();      node = node->children[bit];    }  }   long getMaxXor(long num) {    long maxXor = 0;    shared_ptr<TrieNode> node = root;    for (int i = maxBit; i >= 0; --i) {      const int bit = num >> i & 1;      const int toggleBit = bit ^ 1;      if (node->children[toggleBit] != nullptr) {        maxXor = maxXor | 1L << i;        node = node->children[toggleBit];      } else if (node->children[bit] != nullptr) {        node = node->children[bit];      } else {  // There's nothing in the Bit Trie.        return 0;      }    }    return maxXor;  }  private:  const int maxBit;  shared_ptr<TrieNode> root = make_shared<TrieNode>();}; class Solution { public:  long long maxXor(int n, vector<vector<int>>& edges, vector<int>& values) {    long ans = 0;    vector<vector<int>> tree(n);    vector<long> treeSums(n);     for (const vector<int>& edge : edges) {      const int u = edge[0];      const int v = edge[1];      tree[u].push_back(v);      tree[v].push_back(u);    }     getTreeSum(tree, 0, -1, treeSums, values);    const long maxSubTreeSum =        *max_element(treeSums.begin() + 1, treeSums.end());    const int maxBit = static_cast<int>(log2(maxSubTreeSum));    // Similar to 421. Maximum XOR of Two Numbers in an Array    dfs(tree, 0, -1, treeSums, BitTrie(maxBit), ans);    return ans;  }  private:  // Gets the tree sum rooted at node u.  long getTreeSum(const vector<vector<int>>& tree, int u, int prev,                  vector<long>& treeSums, const vector<int>& values) {    long treeSum = values[u];    for (const int v : tree[u])      if (v != prev)        treeSum += getTreeSum(tree, v, u, treeSums, values);    treeSums[u] = treeSum;    return treeSum;  }   void dfs(const vector<vector<int>>& tree, int u, int prev,           const vector<long>& treeSums, BitTrie&& bitTrie, long& ans) {    for (const int v : 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(tree, v, u, treeSums, std::move(bitTrie), ans);      // Postorder 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 ↗