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: 2110 XORVALUE = 9 218 XORVALUE = 11 24 XORVALUE = 6 Input: 2 / \ 1 4 / \ / \ 10 8 5 10 Output: 12
Approach:

To solve the question mentioned above we have to traverse the tree recursively using preorder 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)

If the node is a leaf node that is left and the right child for the current nodes are NULL then we compute the maxXor, as
maxXor = max(maxXor, curXor).
Below is the implementation of the above approach:
C++
// C++ program to compute the // MaxXor 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 maxxor 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; } 
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
Recommended Posts:
 Maximum Path Sum in a Binary Tree
 Find the maximum path sum between two leaves of a binary tree
 Find the maximum sum leaf to root path in a Binary Tree
 Maximum Consecutive Increasing Path Length in Binary Tree
 Maximum weighted edge in path between two nodes in an Nary tree using binary lifting
 Print all paths of the Binary Tree with maximum element in each path greater than or equal to K
 Minimum and maximum node that lies in the path connecting two nodes in a Binary Tree
 XOR of path between any two nodes in a Binary Tree
 Count of 1's in any path in a Binary Tree
 Print path between any two nodes in a Binary Tree  Set 2
 Print path between any two nodes in a Binary Tree
 Minimum sum path between two leaves of a binary tree
 Longest Path with Same Values in a Binary Tree
 Print all Coprime path of a Binary Tree
 Maximum XOR with given value in the path from root to given node in the tree
 Sort the path from root to a given node in a Binary Tree
 Print path from root to a given node in a binary tree
 Print the first shortest root to leaf path in a Binary Tree
 Shortest path between two nodes in array like representation of binary tree
 Print the longest path from root to leaf in a Binary tree
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.