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:
- Create a tree in level order
- DFS traversal of a tree using recursion
- Maximum Possible Edge Disjoint Spanning Tree From a Complete Graph
- Zig Zag Level order traversal of a tree using single queue
- Level Ancestor Problem
- Preorder Traversal of N-ary Tree Without Recursion
- HashSet vs TreeSet in Java
- Preorder from Inorder and Postorder traversals
- Count pairs in a binary tree whose sum is equal to a given value x
- Find the maximum node at a given level in a binary tree
- Print nodes in top view of Binary Tree | Set 2
- Queries for M-th node in the DFS of subtree
- Lowest Common Ancestor in Parent Array Representation
- Iterative Preorder Traversal of an N-ary Tree
- Amadeus Labs R & D | On Campus (freshers) | Full time+Internship
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.