Maximum XOR with given value in the path from root to given node in the tree

Given a tree with N distinct nodes from the range [1, n] and two integers x and val. The task is to find the maximum value of any node when XORed with x on the path from the root to val.

Examples:

Input: val = 6, x = 4
    1
   / \
  2   3
 /     \
4       5
       /
      6
Output: 7
the path is 1 -> 3 -> 5 -> 6
1 ^ 4 = 5
3 ^ 4 = 7
5 ^ 4 = 1
6 ^ 4 = 2
Maximum is 7

Input: val = 4, x = 1
    1
   / \
  2   3
 /     
4    
Output: 5

Approach:



  • An optimized solution to the problem is to create a parent array to store the parent of each of the node.
  • Start from the given node and keep on going up in the tree using the parent array (this will be helpful when answering a number of queries as only the nodes on the path will be traversed). Take the xor with x of all the nodes in the path till root.
  • The maximum xor calculated for the path is the answer.

Below is the implementation of the above approach:

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
public class GFG {
  
    // Tree node
    static class Node {
        int data;
        Node left, right;
        Node(int data)
        {
            this.data = data;
            left = null;
            right = null;
        }
    }
  
    // Recursive function to update
    // the parent array such that parent[i]
    // stores the parent of i
    static void updateParent(int parent[],
                             Node node)
    {
  
        // If node is null then return
        if (node == null)
            return;
  
        // If left child of the node is not
        // null then set node as the parent
        // of its left child
        if (node.left != null)
            parent[node.left.data] = node.data;
  
        // If right child of the node is not
        // null then set node as the parent
        // of its right child
        if (node.right != null)
            parent[node.right.data] = node.data;
  
        // Recursive call for the
        // children of current node
        updateParent(parent, node.left);
        updateParent(parent, node.right);
    }
  
    // Function to return the maximum value
    // of a node on the path from root to val
    // when Xored with x
    static int getMaxXor(Node root,
                         int n, int val, int x)
    {
  
        // Create the parent array
        int parent[] = new int[n + 1];
        updateParent(parent, root);
  
        // Initialze max with x XOR val
        int max = x ^ val;
  
        // Get to the parent of val
        val = parent[val];
  
        // 0 is the parent of the root
        while (val != 0) {
  
            // Update maximum
            max = Math.max(max, x ^ val);
  
            // Get one level up the tree
            val = parent[val];
        }
        return max;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int n = 6;
        Node root = new Node(1);
        root.left = new Node(2);
        root.right = new Node(3);
        root.left.left = new Node(4);
        root.right.right = new Node(5);
        root.right.right.left = new Node(6);
  
        int val = 6, x = 4;
        System.out.println(getMaxXor(root, n, val, x));
    }
}

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach 
using System;
  
class GFG
{
  
    // Tree node
    public class Node 
    {
        public int data;
        public Node left, right;
        public Node(int data)
        {
            this.data = data;
            left = null;
            right = null;
        }
    }
  
    // Recursive function to update
    // the parent array such that parent[i]
    // stores the parent of i
    static void updateParent(int []parent,
                             Node node)
    {
  
        // If node is null then return
        if (node == null)
            return;
  
        // If left child of the node is not
        // null then set node as the parent
        // of its left child
        if (node.left != null)
            parent[node.left.data] = node.data;
  
        // If right child of the node is not
        // null then set node as the parent
        // of its right child
        if (node.right != null)
            parent[node.right.data] = node.data;
  
        // Recursive call for the
        // children of current node
        updateParent(parent, node.left);
        updateParent(parent, node.right);
    }
  
    // Function to return the maximum value
    // of a node on the path from root to val
    // when Xored with x
    static int getMaxXor(Node root, int n, 
                         int val, int x)
    {
  
        // Create the parent array
        int []parent = new int[n + 1];
        updateParent(parent, root);
  
        // Initialze max with x XOR val
        int max = x ^ val;
  
        // Get to the parent of val
        val = parent[val];
  
        // 0 is the parent of the root
        while (val != 0) 
        {
  
            // Update maximum
            max = Math.Max(max, x ^ val);
  
            // Get one level up the tree
            val = parent[val];
        }
        return max;
    }
  
    // Driver code
    public static void Main(String[] args)
    {
        int n = 6;
        Node root = new Node(1);
        root.left = new Node(2);
        root.right = new Node(3);
        root.left.left = new Node(4);
        root.right.right = new Node(5);
        root.right.right.left = new Node(6);
  
        int val = 6, x = 4;
        Console.WriteLine(getMaxXor(root, n, val, x));
    }
}
  
// This code is contributed by Princi Singh

chevron_right


Output:

7

Time Complexity: O(N)



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.



Improved By : princi singh



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.