Count all pairs of adjacent nodes whose XOR is an odd number
Last Updated :
07 Oct, 2022
Given a Binary Tree as shown below. The task is to count all pair of adjacent nodes whose XOR is an odd number.
Explanation:
Initially, root will be 0, start traversing the tree.
XOR of 15 and 13 will be 2 (Even)
XOR of 13 and 12 will be 1 (Odd)
XOR of 13 and 14 will be 5 (Even)
XOR of 15 and 18 will be 13 (Odd)
XOR of 18 and 17 will be 3 (Odd)
XOR of 18 and 21 will be 7 (Odd)
Therefore, total adjacent pairs with odd XOR = 5
Approach:
- Start traversing the tree from top to down.
- Every time perform XOR operation with the current node data and its adjacent data.
- If XOR of both node is an odd number then increment the count.
Implementation:
C++
#include <iostream>
using namespace std;
struct Node {
int data;
struct Node *left, *right;
};
int countOddXor(Node* root, Node *parent=NULL)
{
if (root == NULL)
return 0;
int res = 0;
if (parent != NULL && (parent->data ^ root->data) % 2)
res++;
return res + countOddXor(root->left, root) +
countOddXor(root->right, root);
}
Node* newNode( int data)
{
Node* temp = new Node;
temp->data = data;
temp->left = NULL;
temp->right = NULL;
return temp;
}
int main()
{
struct Node* root = NULL;
root = newNode(15);
root->left = newNode(13);
root->left->left = newNode(12);
root->left->right = newNode(14);
root->right = newNode(18);
root->right->left = newNode(17);
root->right->right = newNode(21);
printf ( "%d " , countOddXor(root));
return 0;
}
|
Java
class GFG
{
static class Node
{
int data;
Node left, right;
};
static int countOddXor(Node root, Node parent)
{
if (root == null )
return 0 ;
int res = 0 ;
if (parent != null &&
(parent.data ^ root.data) % 2 == 1 )
res++;
return res + countOddXor(root.left, root) +
countOddXor(root.right, root);
}
static Node newNode( int data)
{
Node temp = new Node();
temp.data = data;
temp.left = null ;
temp.right = null ;
return temp;
}
public static void main(String[] args)
{
Node root = null ;
root = newNode( 15 );
root.left = newNode( 13 );
root.left.left = newNode( 12 );
root.left.right = newNode( 14 );
root.right = newNode( 18 );
root.right.left = newNode( 17 );
root.right.right = newNode( 21 );
System.out.printf( "%d " , countOddXor(root, null ));
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def countOddXor(root, parent = None ):
if (root = = None ):
return 0 ;
res = 0 ;
if (parent ! = None and (parent.data ^ root.data) % 2 ):
res + = 1
return res + countOddXor(root.left, root) + countOddXor(root.right, root);
def newNode(data):
temp = Node(data)
return temp
if __name__ = = '__main__' :
root = None ;
root = newNode( 15 );
root.left = newNode( 13 );
root.left.left = newNode( 12 );
root.left.right = newNode( 14 );
root.right = newNode( 18 );
root.right.left = newNode( 17 );
root.right.right = newNode( 21 );
print (countOddXor(root));
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node left, right;
};
static int countOddXor(Node root,
Node parent)
{
if (root == null )
return 0;
int res = 0;
if (parent != null &&
(parent.data ^ root.data) % 2 == 1)
res++;
return res + countOddXor(root.left, root) +
countOddXor(root.right, root);
}
static Node newNode( int data)
{
Node temp = new Node();
temp.data = data;
temp.left = null ;
temp.right = null ;
return temp;
}
public static void Main(String[] args)
{
Node root = null ;
root = newNode(15);
root.left = newNode(13);
root.left.left = newNode(12);
root.left.right = newNode(14);
root.right = newNode(18);
root.right.left = newNode(17);
root.right.right = newNode(21);
Console.WriteLine( "{0} " ,
countOddXor(root, null ));
}
}
|
Javascript
<script>
class Node
{
constructor()
{
this .data = 0;
this .left = null ;
this .right = null ;
}
};
function countOddXor(root, parent)
{
if (root == null )
return 0;
var res = 0;
if (parent != null &&
(parent.data ^ root.data) % 2 == 1)
res++;
return res + countOddXor(root.left, root) +
countOddXor(root.right, root);
}
function newNode( data)
{
var temp = new Node();
temp.data = data;
temp.left = null ;
temp.right = null ;
return temp;
}
var root = null ;
root = newNode(15);
root.left = newNode(13);
root.left.left = newNode(12);
root.left.right = newNode(14);
root.right = newNode(18);
root.right.left = newNode(17);
root.right.right = newNode(21);
document.write(countOddXor(root, null ) + " " );
</script>
|
Time complexity: O(N) where N is the number of nodes of the given Binary Tree.
Auxiliary space: O(N) for recursive stack space.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...