Maximum XOR path of a Binary Tree | Binary tree node ; Function to create a new node ; Function calculate the value of Math . 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 ; Create the binary tree
class Node { constructor ( ) { this . data = 0 ; this . left = null ; this . right = null ; } } ; function newNode ( data ) { var newNode = new Node ; newNode . data = data ; newNode . left = newNode . right = null ; return ( newNode ) ; } function Solve ( root , xr , max_xor ) { xr = xr ^ root . data ; if ( root . left == null && root . right == null ) { max_xor = Math . max ( max_xor , xr ) ; return max_xor ; } if ( root . left != null ) { max_xor = Solve ( root . left , xr , max_xor ) ; } if ( root . right != null ) { max_xor = Solve ( root . right , xr , max_xor ) ; } return max_xor ; } function findMaxXor ( root ) { var xr = 0 , max_xor = 0 ; max_xor = Solve ( root , xr , max_xor ) ; return max_xor ; } var 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 ) ; document . write ( findMaxXor ( root ) ) ;