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 4Output:2Explanation: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 5Input:K = 3, Tree = 4 / \ 3 9 / \ 2 2Output:1

** Approach: **

- Traverse the tree recursively using pre-order traversal.
- For each node keep calculating the bitwise OR of its subtree as:
- If the bitwise OR of any subtree is K, increment the counter variable.
- Print the value in the counter as the required count.
- Count of subtrees in a Binary Tree having XOR value K
- Number of subtrees having odd count of even numbers
- Check if a Binary Tree contains duplicate subtrees of size 2 or more
- Print updated levels of each node of a Complete Binary Tree based on difference in weights of subtrees
- Count subtrees that sum up to a given value x only using single recursive function
- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
- Count of subtrees from an N-ary tree consisting of single colored nodes
- Find largest subtree having identical left and right subtrees
- Find Count of Single Valued Subtrees
- Maximum value of Bitwise AND from root to leaf in a Binary tree
- Largest possible value of M not exceeding N having equal Bitwise OR and XOR between them
- Count of paths in given Binary Tree with odd bitwise AND for Q queries
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Calculate number of nodes in all subtrees | Using DFS
- Find All Duplicate Subtrees
- Subtrees formed after bursting nodes
- Count of pairs having bit size at most X and Bitwise OR equal to X
- Leftover element after performing alternate Bitwise OR and Bitwise XOR operations on adjacent pairs
- Find subsequences with maximum Bitwise AND and Bitwise OR
- Minimum possible Bitwise OR of all Bitwise AND of pairs generated from two given arrays

bitwise OR of its subtree = (bitwise OR of node’s left subtree) | (bitwise OR of node’s right subtree) | (node’s value)

`// C++ program to find the count of ` `// subtrees in a Binary Tree ` `// having bitwise OR value K ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// A binary tree node ` `struct` `Node { ` ` ` `int` `data; ` ` ` `struct` `Node *left, *right; ` `}; ` ` ` `// A utility function to ` `// allocate a new node ` `struct` `Node* newNode(` `int` `data) ` `{ ` ` ` `struct` `Node* newNode = ` `new` `Node; ` ` ` `newNode->data = data; ` ` ` `newNode->left ` ` ` `= newNode->right = NULL; ` ` ` `return` `(newNode); ` `} ` ` ` `// Recursive Function to compute the count ` `int` `rec(Node* root, ` `int` `& res, ` `int` `& k) ` `{ ` ` ` `// Base Case: ` ` ` `// If node is NULL, return 0 ` ` ` `if` `(root == NULL) { ` ` ` `return` `0; ` ` ` `} ` ` ` ` ` `// Calculating the bitwise OR ` ` ` `// of the current subtree ` ` ` `int` `orr = root->data; ` ` ` `orr |= rec(root->left, res, k); ` ` ` `orr |= rec(root->right, res, k); ` ` ` ` ` `// Increment res ` ` ` `// if xr is equal to k ` ` ` `if` `(orr == k) { ` ` ` `res++; ` ` ` `} ` ` ` ` ` `// Return the bitwise OR value ` ` ` `// of the current subtree ` ` ` `return` `orr; ` `} ` ` ` `// Function to find the required count ` `int` `FindCount(Node* root, ` `int` `K) ` `{ ` ` ` `// Initialize result variable 'res' ` ` ` `int` `res = 0; ` ` ` ` ` `// Recursively traverse the tree ` ` ` `// and compute the count ` ` ` `rec(root, res, K); ` ` ` ` ` `// return the count 'res' ` ` ` `return` `res; ` `} ` ` ` `// Driver program ` `int` `main(` `void` `) ` `{ ` ` ` ` ` `/* ` ` ` `2 ` ` ` `/ \ ` ` ` `1 1 ` ` ` `/ \ \ ` ` ` `10 5 4 ` ` ` `*/` ` ` ` ` `// Create the binary tree ` ` ` `// by adding nodes to it ` ` ` `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; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

2

**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)**.

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.

## Recommended Posts:

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.