Maximum value of Bitwise AND from root to leaf in a Binary tree

Given a Binary Tree, the task is to find the maximum value of Bitwise AND from any path from the root node to the leaf node.

Examples:

Input: Below is the given graph:
 

Output: 7
Explanation:
path 1: 15->3->5 = (15 & 3 & 5) = 1
path 2: 15->3->1 =(15 & 3 & 1) = 1
path 3: 15->7->31=(15 & 7 & 31)= 7 (maximum)
path 4: 15->7->9 = (15 & 7 & 9) =1, out of these 7 is the maximum.



Input: Below is the given graph:

Output: 6
Explanation:
Path 1: 31->3->7  =  (31 & 3 & 7)  = 3
Path 2: 31->3->1 =  (31 & 3 & 1)  = 1
Path 3: 31->15->5 =  (31 & 15 & 5)  5
Path 4: 31->15->22 =  (31 & 15 & 22)  = 6, out of these 6 is the maximum.

 

Approach: The idea is to traverse all the path from the root node to the leaf node and calculate the Bitwise AND of all the nodes that occurred in that path. Keep a global variable to update the maximum Bitwise AND value from all the paths.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Initialise to update the maximum
// AND value from all the path
int maxm = 0;
 
// Node structure
struct Node {
    int val;
 
    // Left & right child of the node
    Node *left, *right;
 
    // Intialize consutructor
    Node(int x)
    {
        val = x;
        left = NULL;
        right = NULL;
    }
};
 
// Function to find the maximum value
// of Bitwise AND from root to leaf
// in a Binary tree
void maxm_Anding(Node* root, int ans)
{
    // Check if root is not null
    if (!root)
        return;
 
    if (root->left == NULL
        and root->right == NULL) {
        ans &= root->val;
 
        // Find the maxmimum AND value and
        // store in global maxm varibale
        maxm = max(ans, maxm);
 
        return;
    }
 
    // Traverse left of binary tree
    maxm_Anding(root->left,
                ans & root->val);
 
    // Traverse right of the binary tree
    maxm_Anding(root->right,
                ans & root->val);
}
 
// Driver Code
int main()
{
    // Given Tree
    Node* root = new Node(15);
    root->left = new Node(3);
    root->right = new Node(7);
    root->left->left = new Node(5);
    root->left->right = new Node(1);
    root->right->left = new Node(31);
    root->right->right = new Node(9);
 
    // Function Call
    maxm_Anding(root, root->val);
 
    // Print the maximum AND value
    cout << maxm << endl;
 
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above approach
class GFG{
 
// Initialise to update the maximum
// AND value from all the path
static int maxm = 0;
 
// Node structure
static class Node
{
    int val;
 
    // Left & right child of the node
    Node left, right;
 
    // Intialize consutructor
    Node(int x)
    {
        val = x;
        left = null;
        right = null;
    }
};
 
// Function to find the maximum value
// of Bitwise AND from root to leaf
// in a Binary tree
static void maxm_Anding(Node root, int ans)
{
    // Check if root is not null
    if (root == null)
        return;
 
    if (root.left == null && root.right == null)
    {
        ans &= root.val;
 
        // Find the maxmimum AND value and
        // store in global maxm varibale
        maxm = Math.max(ans, maxm);
 
        return;
    }
 
    // Traverse left of binary tree
    maxm_Anding(root.left,
                ans & root.val);
 
    // Traverse right of the binary tree
    maxm_Anding(root.right,
                ans & root.val);
}
 
// Driver Code
public static void main(String[] args)
{
    // Given Tree
    Node root = new Node(15);
    root.left = new Node(3);
    root.right = new Node(7);
    root.left.left = new Node(5);
    root.left.right = new Node(1);
    root.right.left = new Node(31);
    root.right.right = new Node(9);
 
    // Function Call
    maxm_Anding(root, root.val);
 
    // Print the maximum AND value
    System.out.print(maxm + "\n");
}
}
 
// This code is contributed by sapnasingh4991

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for the above approach
using System;
class GFG{
 
// Initialise to update the maximum
// AND value from all the path
static int maxm = 0;
 
// Node structure
class Node
{
    public int val;
 
    // Left & right child of the node
    public Node left, right;
 
    // Intialize consutructor
    public Node(int x)
    {
        val = x;
        left = null;
        right = null;
    }
};
 
// Function to find the maximum value
// of Bitwise AND from root to leaf
// in a Binary tree
static void maxm_Anding(Node root, int ans)
{
    // Check if root is not null
    if (root == null)
        return;
 
    if (root.left == null && root.right == null)
    {
        ans &= root.val;
 
        // Find the maxmimum AND value and
        // store in global maxm varibale
        maxm = Math.Max(ans, maxm);
 
        return;
    }
 
    // Traverse left of binary tree
    maxm_Anding(root.left,
                ans & root.val);
 
    // Traverse right of the binary tree
    maxm_Anding(root.right,
                ans & root.val);
}
 
// Driver Code
public static void Main(String[] args)
{
    // Given Tree
    Node root = new Node(15);
    root.left = new Node(3);
    root.right = new Node(7);
    root.left.left = new Node(5);
    root.left.right = new Node(1);
    root.right.left = new Node(31);
    root.right.right = new Node(9);
 
    // Function Call
    maxm_Anding(root, root.val);
 
    // Print the maximum AND value
    Console.Write(maxm + "\n");
}
}
 
// This code is contributed by sapnasingh4991

chevron_right


 
 

Output: 

7



 

 

Time Complexity: O(N2)
Auxiliary Space: O(1)

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




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 : sapnasingh4991