Given the binary Tree and the two nodes say ‘a’ and ‘b’, determine whether the two nodes are cousins of each other or not.
Two nodes are cousins of each other if they are at same level and have different parents.
6 / \ 3 5 / \ / \ 7 8 1 3 Say two node be 7 and 1, result is TRUE. Say two nodes are 3 and 5, result is FALSE. Say two nodes are 7 and 5, result is FALSE.
The idea is to find level of one of the nodes. Using the found level, check if ‘a’ and ‘b’ are at this level. If ‘a’ and ‘b’ are at given level, then finally check if they are not children of same parent.
Following is the implementation of the above approach.
Time Complexity of the above solution is O(n) as it does at most three traversals of binary tree.
This article is contributed by Ayush Srivastava. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- Check if a binary tree is subtree of another binary tree | Set 2
- Find Minimum Depth of a Binary Tree
- Minimum no. of iterations to pass information to all nodes in the tree
- Reverse alternate levels of a perfect binary tree
- Serialize and Deserialize a Binary Tree
- 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