Count the number of paths from root to leaf of a Binary tree with given XOR value
Last Updated :
22 Jun, 2021
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++
// C++ program to Count the number of
// path from the root to leaf of a
// Binary tree with given XOR value
#include <bits/stdc++.h>
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
Python3
# Python3 program to Count
# the number of path from
# the root to leaf of a
# Binary tree with given XOR value
# Binary tree node
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
# 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 is None and
root.right is None):
if (xr == K):
res += 1
return
# Check if the left
# node exist in the tree
if (root.left):
Count(root.left, xr)
# Check if the right node
# exist in the tree
if (root.right):
Count(root.right, xr)
return
# Function to find the
# required count
def findCount(root : Node) -> int:
global K, res
xr = 0
# Recursively traverse the tree
# and compute the count
Count(root, xr)
# return the result
return res
# Driver code
if __name__ == "__main__":
# Create the binary tree
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))
# This code is contributed by sanjeev2552
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
JavaScript
<script>
// JavaScript program to Count the number of
// path from the root to leaf of a
// Binary tree with given XOR value
// Binary tree node
class Node {
constructor()
{
this.data = 0;
this.left = null;
this.right = null;
}
};
var res, k;
// Function to create a new node
function newNode(data)
{
var newNode = new Node();
newNode.data = data;
newNode.left
= newNode.right = null;
return (newNode);
}
function Count(root, 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
function findCount(root, K)
{
var xr = 0;
res = 0;
k = K;
// recursively traverse the tree
// and compute the count
Count(root, xr);
// return the result
return res;
}
// Driver code
// Create the binary tree
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).
Similar Reads
Count of root to leaf paths in a Binary Tree that form an AP
Given a Binary Tree, the task is to count all paths from root to leaf which forms an Arithmetic Progression. Examples: Input: Output: 2 Explanation: The paths that form an AP in the given tree from root to leaf are: 1->3->5 (A.P. with common difference 2)1->6->11 (A.P. with common differ
7 min read
Count nodes having highest value in the path from root to itself in a Binary Tree
Given a Binary Tree, the task is to count the number of nodes in the Binary Tree, which are the highest valued node in the path from the root up to that node. Examples: Input: Below is the given Tree: 3 / \ 2 5 / \ 4 6Output: 4Explanation:Root node satisfies the required condition.Node 5 is the high
8 min read
Count nodes having smallest value in the path from root to itself in a Binary Tree
Given a Binary Tree, the task is to count the number of nodes in the given Binary Tree such that the path from the root to that node contains node with value greater than or equal to that node. Examples: Input: 6 / \ 7 4 / \ / \ 3 7 1 2 Output: 5 Explanation: Root node 6 is considered as its the onl
8 min read
Maximum XOR with given value in the path from root to given node in the tree
Given a tree with N distinct nodes from the range [1, n] and two integers x and val. The task is to find the maximum value of any node when XORed with x on the path from the root to val. Examples: Input: val = 6, x = 4 1 / \ 2 3 / \ 4 5 / 6 Output: 7 the path is 1 -> 3 -> 5 -> 6 1 ^ 4 = 5 3
9 min read
Print all the root-to-leaf paths of a Binary Tree whose XOR is non-zero
Given a Binary Tree, the task is to print all root-to-leaf paths of this tree whose xor value is non-zero. Examples: Input: 10 / \ 10 3 / \ 10 3 / \ / \ 7 3 42 13 / 7 Output: 10 3 10 7 10 3 3 42 Explanation: All the paths in the given binary tree are : {10, 10} xor value of the path is = 10 ^ 10 = 0
11 min read
Queries to calculate sum of the path from root to a given node in given Binary Tree
Given an infinite complete binary tree rooted at node 1, where every ith node has two children, with values 2 * i and 2 * (i + 1). Given another array arr[] consisting of N positive integers, the task for each array element arr[i] is to find the sum of the node values that occur in a path from the r
10 min read
Path from the root node to a given node in an N-ary Tree
Given an integer N and an N-ary Tree of the following form: Every node is numbered sequentially, starting from 1, till the last level, which contains the node N.The nodes at every odd level contains 2 children and nodes at every even level contains 4 children. The task is to print the path from the
10 min read
Print path from a node to root of given Complete Binary Tree
Given an integer N, the task is to find the path from the Nth node to the root of a Binary Tree of the following form: The Binary Tree is a Complete Binary Tree up to the level of the Nth node.The nodes are numbered 1 to N, starting from the root as 1.The structure of the Tree is as follows: 1 / \ 2
4 min read
Maximize count of set bits in a root to leaf path in a binary tree
Given a binary tree, the task is to find the total count of set bits in the node values of all the root to leaf paths and print the maximum among them. Examples: Input: Output: 12Explanation:Path 1: 15(1111)->3(0011)->5(0101) = 8Path 2: 15(1111)->3(0011)->1(0001) = 7Path 3: 15(01111)-
6 min read
Maximum value of Bitwise AND from root to leaf in a Binary tree
Given a Binary Tree, the task is to find the maximum value of Bitwise AND from any path from the root node to the leaf node. Examples: Input: Below is the given graph: Output: 7Explanation:path 1: 15->3->5 = (15 & 3 & 5) = 1path 2: 15->3->1 =(15 & 3 & 1) = 1path 3: 15-
7 min read