Given a value K and a binary tree, we have to find out the total number of paths from the 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.
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 the 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++
#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);
}
void Count(Node* root, int xr, int & res, int & k)
{
xr = xr ^ root->data;
if (root->left == NULL && root->right == NULL) {
if (xr == k) {
res++;
}
return ;
}
if (root->left != NULL) {
Count(root->left, xr, res, k);
}
if (root->right != NULL) {
Count(root->right, xr, res, k);
}
return ;
}
int findCount(Node* root, int K)
{
int res = 0, xr = 0;
Count(root, xr, res, K);
return res;
}
int main( void )
{
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
import java.util.*;
class GFG{
static class Node {
int data;
Node left, right;
};
static int res, k;
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)
{
xr = xr ^ root.data;
if (root.left == null && root.right == null ) {
if (xr == k) {
res++;
}
return ;
}
if (root.left != null ) {
Count(root.left, xr);
}
if (root.right != null ) {
Count(root.right, xr);
}
return ;
}
static int findCount(Node root, int K)
{
int xr = 0 ;
res = 0 ;
k = K;
Count(root, xr);
return res;
}
public static void main(String[] args)
{
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));
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def Count(root : Node,
xr : int ) - > None :
global K, res
xr = xr ^ root.data
if (root.left is None and
root.right is None ):
if (xr = = K):
res + = 1
return
if (root.left):
Count(root.left, xr)
if (root.right):
Count(root.right, xr)
return
def findCount(root : Node) - > int :
global K, res
xr = 0
Count(root, xr)
return res
if __name__ = = "__main__" :
root = Node( 2 )
root.left = Node( 1 )
root.right = Node( 4 )
root.left.left = Node( 10 )
root.left.right = Node( 5 )
K = 6
res = 0
print (findCount(root))
|
C#
using System;
class GFG{
class Node {
public int data;
public Node left, right;
};
static int res, k;
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)
{
xr = xr ^ root.data;
if (root.left == null && root.right == null ) {
if (xr == k) {
res++;
}
return ;
}
if (root.left != null ) {
Count(root.left, xr);
}
if (root.right != null ) {
Count(root.right, xr);
}
return ;
}
static int findCount(Node root, int K)
{
int xr = 0;
res = 0;
k = K;
Count(root, xr);
return res;
}
public static void Main(String[] args)
{
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));
}
}
|
Javascript
<script>
class Node {
constructor()
{
this .data = 0;
this .left = null ;
this .right = null ;
}
};
var res, k;
function newNode(data)
{
var newNode = new Node();
newNode.data = data;
newNode.left
= newNode.right = null ;
return (newNode);
}
function Count(root, xr)
{
xr = xr ^ root.data;
if (root.left == null && root.right == null ) {
if (xr == k) {
res++;
}
return ;
}
if (root.left != null ) {
Count(root.left, xr);
}
if (root.right != null ) {
Count(root.right, xr);
}
return ;
}
function findCount(root, K)
{
var xr = 0;
res = 0;
k = K;
Count(root, xr);
return res;
}
var root = newNode(2);
root.left = newNode(1);
root.right = newNode(4);
root.left.left = newNode(10);
root.left.right = newNode(5);
var K = 6;
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: As in the above approach there is no extra space used, therefore the Auxiliary Space complexity will be O(1).
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
22 Jun, 2021
Like Article
Save Article