# Count of subtrees in a Binary Tree having XOR value K

Given a value K and a binary tree, the task is to find out number of subtrees having XOR of all its elements equal to K.

Examples:

```Input  K = 5, Tree =
2
/ \
1   9
/ \
10  5
Output: 2
Explanation:
Subtree 1:
5
It has only one element i.e. 5.
So XOR of subtree = 5
Subtree 1:
2
/ \
1   9
/ \
10  5
It has elements 2, 1, 9, 10, 5.
So XOR of subtree = 2 ^ 1 ^ 9 ^ 10 ^ 5 = 5

Input  K = 3, Tree =
4
/ \
3   9
/ \
2   2
Output: 1
Explanation
Subtree:
3
/ \
2   2
It has elements 3, 2, 2.
So XOR of subtree = 3 ^ 2 ^ 2 = 3
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach

1. Traverse the tree recursively using pre-order traversal.
2. For each node keep calculating the XOR of its subtree as:

XOR of its subtree = (XOR of node’s left subtree) ^ (XOR of nodes’s right subtree) ^ (node’s value)

3. If the XOR of any subtree is K, increment the counter variable.
4. Print the value in counter as the required count

Below is the implementation of the above approach:

## C++

 `// C++ program to find the count of ` `// subtrees in a Binary Tree ` `// having XOR value K ` ` `  `#include ` `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); ` `} ` ` `  `int` `rec(Node* root, ``int``& res, ``int``& k) ` `{ ` `    ``// Base Case: ` `    ``// If node is NULL, return 0 ` `    ``if` `(root == NULL) { ` `        ``return` `0; ` `    ``} ` ` `  `    ``// Calculating the XOR ` `    ``// of the current subtree ` `    ``int` `xr = root->data; ` `    ``xr ^= rec(root->left, res, k); ` `    ``xr ^= rec(root->right, res, k); ` ` `  `    ``// Increment res ` `    ``// if xr is equal to k ` `    ``if` `(xr == k) { ` `        ``res++; ` `    ``} ` ` `  `    ``// Return the XOR value ` `    ``// of the current subtree ` `    ``return` `xr; ` `} ` ` `  `// 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   9 ` `     ``/ \ ` `    ``10  5 ` `    ``*/` ` `  `    ``// Create the binary tree ` `    ``// by adding nodes to it ` `    ``struct` `Node* root = newNode(2); ` `    ``root->left = newNode(1); ` `    ``root->right = newNode(9); ` `    ``root->left->left = newNode(10); ` `    ``root->left->right = newNode(5); ` ` `  `    ``int` `K = 5; ` ` `  `    ``cout << findCount(root, K); ` `    ``return` `0; ` `} `

## Java

 `// Java program to find the count of ` `// subtrees in a Binary Tree ` `// having XOR value K ` `import` `java.util.*; ` ` `  `class` `GFG{ ` ` `  `    ``// A binary tree node ` `    ``static` `class` `Node ` `    ``{  ` `        ``int` `data;  ` `        ``Node left,right;  ` `    ``};  ` `    ``static` `int` `res; ` `    ``static` `int` `k; ` ` `  `    ``// A utility function to ` `    ``// allocate a new node ` `    ``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) ` `    ``{ ` `        ``// Base Case: ` `        ``// If node is null, return 0 ` `        ``if` `(root == ``null``) { ` `            ``return` `0``; ` `        ``} ` `     `  `        ``// Calculating the XOR ` `        ``// of the current subtree ` `        ``int` `xr = (root.data);``//^rec(root.left)^rec(root.right); ` `        ``xr ^= rec(root.left); ` `        ``xr ^= rec(root.right); ` `         `  `        ``// Increment res ` `        ``// if xr is equal to k ` `        ``if` `(xr == k) { ` `            ``res++; ` `        ``} ` `     `  `        ``// Return the XOR value ` `        ``// of the current subtree ` `        ``return` `xr; ` `    ``} ` `     `  `    ``// Function to find the required count ` `    ``static` `int` `findCount(Node root, ``int` `K) ` `    ``{ ` `        ``// Initialize result variable 'res' ` `        ``res = ``0``; ` `        ``k = K; ` ` `  `        ``// Recursively traverse the tree ` `        ``// and compute the count ` `        ``rec(root); ` ` `  `        ``// return the count 'res' ` `        ``return` `res; ` `    ``} ` `     `  `    ``// Driver program ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `     `  `        ``/*  ` `            ``2 ` `        ``/ \ ` `        ``1 9 ` `        ``/ \ ` `        ``10 5 ` `        ``*/` `     `  `        ``// Create the binary tree ` `        ``// by adding nodes to it ` `        ``Node root = newNode(``2``); ` `        ``root.left = newNode(``1``); ` `        ``root.right = newNode(``9``); ` `        ``root.left.left =newNode(``10``); ` `        ``root.left.right = newNode(``5``); ` `     `  `        ``int` `K = ``5``; ` `     `  `        ``System.out.println(findCount(root, K));     ` `    ``} ` `} ` ` `  ` `  `// This code is contributed by AbhiThakur `

## C#

 `// C# program to find the count of ` `// subtrees in a Binary Tree ` `// having XOR value K ` `using` `System; ` ` `  `public` `class` `GFG{ ` `  `  `    ``// A binary tree node ` `    ``class` `Node ` `    ``{  ` `        ``public` `int` `data;  ` `        ``public` `Node left,right;  ` `    ``};  ` `    ``static` `int` `res; ` `    ``static` `int` `k; ` `  `  `    ``// A utility function to ` `    ``// allocate a new node ` `    ``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) ` `    ``{ ` `        ``// Base Case: ` `        ``// If node is null, return 0 ` `        ``if` `(root == ``null``) { ` `            ``return` `0; ` `        ``} ` `      `  `        ``// Calculating the XOR ` `        ``// of the current subtree ` `        ``int` `xr = (root.data);``//^rec(root.left)^rec(root.right); ` `        ``xr ^= rec(root.left); ` `        ``xr ^= rec(root.right); ` `          `  `        ``// Increment res ` `        ``// if xr is equal to k ` `        ``if` `(xr == k) { ` `            ``res++; ` `        ``} ` `      `  `        ``// Return the XOR value ` `        ``// of the current subtree ` `        ``return` `xr; ` `    ``} ` `      `  `    ``// Function to find the required count ` `    ``static` `int` `findCount(Node root, ``int` `K) ` `    ``{ ` `        ``// Initialize result variable 'res' ` `        ``res = 0; ` `        ``k = K; ` `  `  `        ``// Recursively traverse the tree ` `        ``// and compute the count ` `        ``rec(root); ` `  `  `        ``// return the count 'res' ` `        ``return` `res; ` `    ``} ` `      `  `    ``// Driver program ` `    ``public` `static` `void` `Main(String []args) ` `    ``{ ` `      `  `        ``/*  ` `            ``2 ` `        ``/ \ ` `        ``1 9 ` `        ``/ \ ` `        ``10 5 ` `        ``*/` `      `  `        ``// Create the binary tree ` `        ``// by adding nodes to it ` `        ``Node root = newNode(2); ` `        ``root.left = newNode(1); ` `        ``root.right = newNode(9); ` `        ``root.left.left =newNode(10); ` `        ``root.left.right = newNode(5); ` `      `  `        ``int` `K = 5; ` `      `  `        ``Console.WriteLine(findCount(root, K));     ` `    ``} ` `} ` `  `  `// This code is contributed by sapnasingh4991 `

Output:

```2
```

Performance Analysis:

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.

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.

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.