Maximum value of Bitwise AND from root to leaf in a Binary tree
Last Updated :
03 Nov, 2021
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++
#include <bits/stdc++.h>
using namespace std;
int maxm = 0;
struct Node {
int val;
Node *left, *right;
Node( int x)
{
val = x;
left = NULL;
right = NULL;
}
};
void maxm_Anding(Node* root, int ans)
{
if (!root)
return ;
if (root->left == NULL
and root->right == NULL) {
ans &= root->val;
maxm = max(ans, maxm);
return ;
}
maxm_Anding(root->left,
ans & root->val);
maxm_Anding(root->right,
ans & root->val);
}
int main()
{
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);
maxm_Anding(root, root->val);
cout << maxm << endl;
return 0;
}
|
Java
class GFG{
static int maxm = 0 ;
static class Node
{
int val;
Node left, right;
Node( int x)
{
val = x;
left = null ;
right = null ;
}
};
static void maxm_Anding(Node root, int ans)
{
if (root == null )
return ;
if (root.left == null && root.right == null )
{
ans &= root.val;
maxm = Math.max(ans, maxm);
return ;
}
maxm_Anding(root.left,
ans & root.val);
maxm_Anding(root.right,
ans & root.val);
}
public static void main(String[] args)
{
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 );
maxm_Anding(root, root.val);
System.out.print(maxm + "\n" );
}
}
|
Python3
maxm = 0
class Node:
def __init__( self , x):
self .val = x
self .left = None
self .right = None
def maxm_Anding(root: Node, ans: int ) - > None :
global maxm
if not root:
return
if (root.left is None and
root.right is None ):
ans & = root.val
maxm = max (ans, maxm)
return
maxm_Anding(root.left, ans & root.val)
maxm_Anding(root.right, ans & root.val)
if __name__ = = "__main__" :
root = Node( 15 )
root.left = Node( 3 )
root.right = Node( 7 )
root.left.left = Node( 5 )
root.left.right = Node( 1 )
root.right.left = Node( 31 )
root.right.right = Node( 9 )
maxm_Anding(root, root.val)
print (maxm)
|
C#
using System;
class GFG{
static int maxm = 0;
class Node
{
public int val;
public Node left, right;
public Node( int x)
{
val = x;
left = null ;
right = null ;
}
};
static void maxm_Anding(Node root, int ans)
{
if (root == null )
return ;
if (root.left == null && root.right == null )
{
ans &= root.val;
maxm = Math.Max(ans, maxm);
return ;
}
maxm_Anding(root.left,
ans & root.val);
maxm_Anding(root.right,
ans & root.val);
}
public static void Main(String[] args)
{
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);
maxm_Anding(root, root.val);
Console.Write(maxm + "\n" );
}
}
|
Javascript
<script>
let maxm = 0;
class Node
{
constructor(x)
{
this .val = x;
this .left = null ;
this .right = null ;
}
}
var root;
function maxm_Anding(root, ans)
{
if (!root)
return ;
if (root.left == null &&
root.right == null )
{
ans &= root.val;
maxm = Math.max(ans, maxm);
return ;
}
maxm_Anding(root.left,
ans & root.val);
maxm_Anding(root.right,
ans & root.val);
}
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);
maxm_Anding(root, root.val);
document.write(maxm);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...