Given a Binary Tree as shown below. The task is to count all pair of adjacent nodes whose XOR is an odd number.
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
- 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.
Below is the implementation of the above approach:
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.
- Count pairs with Bitwise XOR as ODD number
- All pairs whose xor gives unique prime
- Count pairs with Odd XOR
- Print levels with odd number of nodes and even number of nodes
- Count the nodes in the given tree whose sum of digits of weight is odd
- Sum of all odd nodes in the path connecting two given nodes
- Leftover element after performing alternate Bitwise OR and Bitwise XOR operations on adjacent pairs
- Count set bits in Bitwise XOR of all adjacent elements upto N
- Length of longest subsequence whose XOR value is odd
- Count nodes having Bitwise XOR of all edges in their path from the root equal to K
- Print odd positioned nodes of odd levels in level order of the given binary tree
- Count all pairs with given XOR
- Find the original Array using XOR values of all adjacent elements
- Given a set, find XOR of the XOR's of all subsets.
- Count of all prime weight nodes between given nodes in the given Tree
- Count pairs with Bitwise XOR as EVEN number
- Number whose XOR sum with given array is a given number k
- Print all the levels with odd and even number of nodes in it | Set-2
- Count nodes in the given tree whose weight is a fibonacci number
- Count the nodes whose sum with X is a Fibonacci number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.