Maximum XOR path of a Binary Tree

Given a Binary Tree, the task is to find the maximum of all the XOR value of all the nodes in the path from the root to leaf.

Examples:

Input: 
       2
      / \
     1   4
    / \   
   10  8   
Output: 11
Explanation:
All the paths are: 
2-1-10 XOR-VALUE = 9
2-1-8 XOR-VALUE = 11
2-4 XOR-VALUE = 6

Input: 
        2
      /   \
     1     4
    / \   / \
   10  8 5  10
Output: 12

Approach:

  1. To solve the question mentioned above we have to traverse the tree recursively using pre-order traversal. For each node keep calculating the XOR of the path from root till the current node.

    XOR of current node’s path = (XOR of the path till the parent) ^ (current node value)



  2. If the node is a leaf node that is left and the right child for the current nodes are NULL then we compute the max-Xor, as

    max-Xor = max(max-Xor, cur-Xor).

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to compute the
// Max-Xor value of path from
// the root to leaf of a Binary tree
  
#include <bits/stdc++.h>
using namespace std;
  
// Binary tree node
struct Node {
    int data;
  
    struct Node *left, *right;
};
  
// Function to create a new node
struct Node* newNode(int data)
{
    struct Node* newNode = new Node;
  
    newNode->data = data;
  
    newNode->left
        = newNode->right = NULL;
  
    return (newNode);
}
  
// Function calculate the
// value of max-xor
void Solve(Node* root, int xr,
           int& max_xor)
{
  
    // Updating the xor value
    // with the xor of the
    // path from root to
    // the node
    xr = xr ^ root->data;
  
    // Check if node is leaf node
    if (root->left == NULL
        && root->right == NULL) {
  
        max_xor = max(max_xor, xr);
        return;
    }
  
    // Check if the left
    // node exist in the tree
    if (root->left != NULL) {
        Solve(root->left, xr,
              max_xor);
    }
  
    // Check if the right node
    // exist in the tree
    if (root->right != NULL) {
        Solve(root->right, xr,
              max_xor);
    }
  
    return;
}
  
// Function to find the
// required count
int findMaxXor(Node* root)
{
  
    int xr = 0, max_xor = 0;
  
    // Recursively traverse
    // the tree and compute
    // the max_xor
    Solve(root, xr, max_xor);
  
    // Return the result
    return max_xor;
}
  
// Driver code
int main(void)
{
    // Create the binary tree
    struct Node* root = newNode(2);
    root->left = newNode(1);
    root->right = newNode(4);
    root->left->left = newNode(10);
    root->left->right = newNode(8);
    root->right->left = newNode(5);
    root->right->right = newNode(10);
  
    cout << findMaxXor(root);
  
    return 0;
}

chevron_right


Output:

12

Time Complexity: We are iterating over each node only once, therefore it will take O(N) time where N is the number of nodes in the Binary tree.
Auxiliary Space Complexity: The Auxillary Space complexity will be O(1), as there is no extra space used

competitive-programming-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.