Count of subtrees in a Binary Tree having bitwise OR value K
Last Updated :
15 Jun, 2021
Given a value K and a binary tree, the task is to find out the number of subtrees having bitwise OR of all its elements equal to K.
Examples:
Input: K = 5, Tree = 2
/ \
1 1
/ \ \
10 5 4
Output: 2
Explanation:
Subtree 1:
5
It has only one element i.e. 5.
So bitwise OR of subtree = 5
Subtree 2:
1
\
4
it has 2 elements and bitwise OR of them is also 5
Input: K = 3, Tree = 4
/ \
3 9
/ \
2 2
Output: 1
Approach:
- Traverse the tree recursively using pre-order traversal.
- For each node keep calculating the bitwise OR of its subtree as:
bitwise OR of its subtree = (bitwise OR of node’s left subtree) | (bitwise OR of node’s right subtree) | (node’s value)
- If the bitwise OR of any subtree is K, increment the counter variable.
- Print the value in the counter as the required count.
C++
#include <bits/stdc++.h>
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);
}
int rec(Node* root, int & res, int & k)
{
if (root == NULL) {
return 0;
}
int orr = root->data;
orr |= rec(root->left, res, k);
orr |= rec(root->right, res, k);
if (orr == k) {
res++;
}
return orr;
}
int FindCount(Node* root, int K)
{
int res = 0;
rec(root, res, K);
return res;
}
int main( void )
{
struct Node* root = newNode(2);
root->left = newNode(1);
root->right = newNode(1);
root->right->right = newNode(4);
root->left->left = newNode(10);
root->left->right = newNode(5);
int K = 5;
cout << FindCount(root, K);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static class Node
{
public int data;
public Node left, right;
};
static int res;
static int k;
static Node newNode( int data)
{
Node newNode = new Node();
newNode.data = data;
newNode.left = null ;
newNode.right = null ;
return newNode;
}
static int rec(Node root)
{
if (root == null )
{
return 0 ;
}
int xr = (root.data);
xr |= rec(root.left);
xr |= rec(root.right);
if (xr == k)
{
res++;
}
return xr;
}
static int findCount(Node root, int K)
{
res = 0 ;
k = K;
rec(root);
return res;
}
public static void main (String[] args)
{
Node root = newNode( 2 );
root.left = newNode( 1 );
root.right = newNode( 1 );
root.right.right = newNode( 4 );
root.left.left =newNode( 10 );
root.left.right = newNode( 5 );
int K = 5 ;
System.out.println(findCount(root, K));
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def newNode(data):
temp = Node(data)
return temp
def rec(root, res, k):
if (root = = None ):
return [ 0 , res];
orr = root.data;
tmp, res = rec(root.left, res, k);
orr | = tmp
tmp, res = rec(root.right, res, k);
orr | = tmp
if (orr = = k):
res + = 1
return orr, res;
def FindCount(root, K):
res = 0 ;
tmp,res = rec(root, res, K);
return res;
if __name__ = = '__main__' :
root = newNode( 2 );
root.left = newNode( 1 );
root.right = newNode( 1 );
root.right.right = newNode( 4 );
root.left.left = newNode( 10 );
root.left.right = newNode( 5 );
K = 5 ;
print (FindCount(root, K))
|
C#
using System;
class GFG{
class Node
{
public int data;
public Node left, right;
};
static int res;
static int k;
static Node newNode( int data)
{
Node newNode = new Node();
newNode.data = data;
newNode.left= null ;
newNode.right = null ;
return newNode;
}
static int rec(Node root)
{
if (root == null )
{
return 0;
}
int xr = (root.data);
xr |= rec(root.left);
xr |= rec(root.right);
if (xr == k)
{
res++;
}
return xr;
}
static int findCount(Node root, int K)
{
res = 0;
k = K;
rec(root);
return res;
}
public static void Main(String []args)
{
Node root = newNode(2);
root.left = newNode(1);
root.right = newNode(1);
root.right.right = newNode(4);
root.left.left =newNode(10);
root.left.right = newNode(5);
int K = 5;
Console.WriteLine(findCount(root, K));
}
}
|
Javascript
<script>
class Node
{
constructor(key)
{
this .data = key;
this .left = this .right = null ;
}
}
let res, k;
function rec(root)
{
if (root == null )
{
return 0;
}
let xr = (root.data);
xr |= rec(root.left);
xr |= rec(root.right);
if (xr == k)
{
res++;
}
return xr;
}
function findCount(root, K)
{
res = 0;
k = K;
rec(root);
return res;
}
let root = new Node(2);
root.left = new Node(1);
root.right = new Node(1);
root.right.right = new Node(4);
root.left.left = new Node(10);
root.left.right = new Node(5);
let K = 5;
document.write(findCount(root, K));
</script>
|
Time Complexity: As in the above approach, we are iterating over each node only once, therefore it will take O(N) time where N is the number of nodes in the Binary tree.
Auxiliary Space Complexity: As in the above approach there is no extra space used, therefore the Auxiliary Space complexity will be O(1).
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...