# Count the number of paths from root to leaf of a Binary tree with given XOR value

Given a value K and a binary tree, we have to find out the total number of paths from root to leaf nodes having XOR of all its nodes along the path equal to K.

Examples:

```Input: K = 6
2
/ \
1   4
/ \
10  5
Output: 2
Explanation:
Subtree 1:
2
\
4
This particular path has 2 nodes, 2 and 4
and (2 xor 4) = 6.

Subtree 2:
2
/
1
\
5
This particular path has 3 nodes; 2, 1 and 5
and (2 xor 1 xor 5) = 6.
```

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

Approach:

To solve the question mentioned above we have to traverse the tree recursively using pre-order traversal. For each node keep calculating the XOR of the path from root till the current node.

XOR of current node’s path = (XOR of the path till the parent) ^ (current node value)

If the node is a leaf node that is left and the right child for the current nodes are NULL then we check if the xor value of path is K, if it is then we increase the count otherwise we do nothing. Finally, print the value in the count.

Below is the implementation of the above approach:

## C++

 `// C++ program to Count the number of ` `// path from the root to leaf of a ` `// Binary tree with given XOR value ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Binary tree node ` `struct` `Node { ` `    ``int` `data; ` ` `  `    ``struct` `Node *left, *right; ` `}; ` ` `  `// Function to create a new node ` `struct` `Node* newNode(``int` `data) ` `{ ` `    ``struct` `Node* newNode = ``new` `Node; ` ` `  `    ``newNode->data = data; ` ` `  `    ``newNode->left ` `        ``= newNode->right = NULL; ` ` `  `    ``return` `(newNode); ` `} ` ` `  `void` `Count(Node* root, ``int` `xr, ``int``& res, ``int``& k) ` `{ ` ` `  `    ``// updating the xor value ` `    ``// with the xor of the path from ` `    ``// root to the node ` `    ``xr = xr ^ root->data; ` ` `  `    ``// check if node is leaf node ` `    ``if` `(root->left == NULL && root->right == NULL) { ` ` `  `        ``if` `(xr == k) { ` `            ``res++; ` `        ``} ` `        ``return``; ` `    ``} ` ` `  `    ``// check if the left ` `    ``// node exist in the tree ` `    ``if` `(root->left != NULL) { ` `        ``Count(root->left, xr, res, k); ` `    ``} ` ` `  `    ``// check if the right node ` `    ``// exist in the tree ` `    ``if` `(root->right != NULL) { ` `        ``Count(root->right, xr, res, k); ` `    ``} ` ` `  `    ``return``; ` `} ` ` `  `// Function to find the required count ` `int` `findCount(Node* root, ``int` `K) ` `{ ` ` `  `    ``int` `res = 0, xr = 0; ` ` `  `    ``// recursively traverse the tree ` `    ``// and compute the count ` `    ``Count(root, xr, res, K); ` ` `  `    ``// return the result ` `    ``return` `res; ` `} ` ` `  `// Driver code ` `int` `main(``void``) ` `{ ` `    ``// Create the binary tree ` `    ``struct` `Node* root = newNode(2); ` `    ``root->left = newNode(1); ` `    ``root->right = newNode(4); ` `    ``root->left->left = newNode(10); ` `    ``root->left->right = newNode(5); ` ` `  `    ``int` `K = 6; ` ` `  `    ``cout << findCount(root, K); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to Count the number of ` `// path from the root to leaf of a ` `// Binary tree with given XOR value ` `import` `java.util.*; ` ` `  `class` `GFG{ ` `  `  `// Binary tree node ` `static` `class` `Node { ` `    ``int` `data; ` `  `  `    ``Node left, right; ` `}; ` `static` `int` `res, k;  ` ` `  `// Function to create a new node ` `static` `Node newNode(``int` `data) ` `{ ` `    ``Node newNode = ``new` `Node(); ` `  `  `    ``newNode.data = data; ` `  `  `    ``newNode.left ` `        ``= newNode.right = ``null``; ` `  `  `    ``return` `(newNode); ` `} ` `  `  `static` `void` `Count(Node root, ``int` `xr) ` `{ ` `  `  `    ``// updating the xor value ` `    ``// with the xor of the path from ` `    ``// root to the node ` `    ``xr = xr ^ root.data; ` `  `  `    ``// check if node is leaf node ` `    ``if` `(root.left == ``null` `&& root.right == ``null``) { ` `  `  `        ``if` `(xr == k) { ` `            ``res++; ` `        ``} ` `        ``return``; ` `    ``} ` `  `  `    ``// check if the left ` `    ``// node exist in the tree ` `    ``if` `(root.left != ``null``) { ` `        ``Count(root.left, xr); ` `    ``} ` `  `  `    ``// check if the right node ` `    ``// exist in the tree ` `    ``if` `(root.right != ``null``) { ` `        ``Count(root.right, xr); ` `    ``} ` `  `  `    ``return``; ` `} ` `  `  `// Function to find the required count ` `static` `int` `findCount(Node root, ``int` `K) ` `{ ` `  `  `    ``int` `xr = ``0``; ` `    ``res = ``0``; ` `    ``k = K; ` ` `  `    ``// recursively traverse the tree ` `    ``// and compute the count ` `    ``Count(root, xr); ` `  `  `    ``// return the result ` `    ``return` `res; ` `} ` `  `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``// Create the binary tree ` `    ``Node root = newNode(``2``); ` `    ``root.left = newNode(``1``); ` `    ``root.right = newNode(``4``); ` `    ``root.left.left = newNode(``10``); ` `    ``root.left.right = newNode(``5``); ` `  `  `    ``int` `K = ``6``; ` `  `  `    ``System.out.print(findCount(root, K)); ` `} ` `} ` ` `  `// This code is contributed by 29AjayKumar `

## C#

 `// C# program to Count the number of ` `// path from the root to leaf of a ` `// Binary tree with given XOR value ` `using` `System; ` ` `  `class` `GFG{ ` `   `  `// Binary tree node ` `class` `Node { ` `    ``public` `int` `data; ` `   `  `    ``public` `Node left, right; ` `}; ` `static` `int` `res, k;  ` `  `  `// Function to create a new node ` `static` `Node newNode(``int` `data) ` `{ ` `    ``Node newNode = ``new` `Node(); ` `   `  `    ``newNode.data = data; ` `   `  `    ``newNode.left ` `        ``= newNode.right = ``null``; ` `   `  `    ``return` `(newNode); ` `} ` `   `  `static` `void` `Count(Node root, ``int` `xr) ` `{ ` `   `  `    ``// updating the xor value ` `    ``// with the xor of the path from ` `    ``// root to the node ` `    ``xr = xr ^ root.data; ` `   `  `    ``// check if node is leaf node ` `    ``if` `(root.left == ``null` `&& root.right == ``null``) { ` `   `  `        ``if` `(xr == k) { ` `            ``res++; ` `        ``} ` `        ``return``; ` `    ``} ` `   `  `    ``// check if the left ` `    ``// node exist in the tree ` `    ``if` `(root.left != ``null``) { ` `        ``Count(root.left, xr); ` `    ``} ` `   `  `    ``// check if the right node ` `    ``// exist in the tree ` `    ``if` `(root.right != ``null``) { ` `        ``Count(root.right, xr); ` `    ``} ` `   `  `    ``return``; ` `} ` `   `  `// Function to find the required count ` `static` `int` `findCount(Node root, ``int` `K) ` `{ ` `   `  `    ``int` `xr = 0; ` `    ``res = 0; ` `    ``k = K; ` `  `  `    ``// recursively traverse the tree ` `    ``// and compute the count ` `    ``Count(root, xr); ` `   `  `    ``// return the result ` `    ``return` `res; ` `} ` `   `  `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``// Create the binary tree ` `    ``Node root = newNode(2); ` `    ``root.left = newNode(1); ` `    ``root.right = newNode(4); ` `    ``root.left.left = newNode(10); ` `    ``root.left.right = newNode(5); ` `   `  `    ``int` `K = 6; ` `   `  `    ``Console.Write(findCount(root, K)); ` `} ` `} ` ` `  `// This code is contributed by Princi Singh `

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: As in the above approach there is no extra space used, therefore the Auxillary Space complexity will be O(1).

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

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 : 29AjayKumar, princi singh

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.