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