Given a binary tree and the two nodes say ‘a’ and ‘b’, determine whether two given 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.
A solution in Set-1 that finds whether given nodes are cousins or not by performing three traversals of binary tree has been discussed. The problem can be solved by performing level order traversal. The idea is to use a queue to perform level order traversal, in which each queue element is a pair of node and parent of that node. For each node visited in level order traversal, check if that node is either first given node or second given node. If any node is found store parent of that node. While performing level order traversal, one level is traversed at a time. If both nodes are found in given level, then their parent values are compared to check if they are siblings or not. If one node is found in given level and another is not found, then given nodes are not cousins.
Below is the implementation of above approach:
Time Complexity: O(n)
Auxiliary Space: O(n)
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.
- Check if two nodes are cousins in a Binary Tree
- Print cousins of a given node in Binary Tree
- Sum of cousins of a given node in a Binary Tree
- Print cousins of a given node in Binary Tree | Single Traversal
- Count the nodes of the tree which make a pangram when concatenated with the sub-tree nodes
- Check if two nodes in a Binary Tree are siblings
- Check if all nodes of the Binary Tree can be represented as sum of two primes
- Common nodes in the inorder sequence of a tree between given two nodes in O(1) space
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Check if a binary tree is subtree of another binary tree | Set 1
- Check if a binary tree is subtree of another binary tree | Set 2
- Check whether a binary tree is a full binary tree or not
- Check whether a binary tree is a full binary tree or not | Iterative Approach
- Check whether a given binary tree is skewed binary tree or not?
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Check if max sum level of Binary tree divides tree into two equal sum halves
- Construct XOR tree by Given leaf nodes of Perfect Binary Tree
- Minimum difference between any two weighted nodes in Sum Tree of the given Tree
- Find distance between two nodes of a Binary Tree
- Print nodes between two given level numbers of a binary tree
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.