Given a binary tree and a node, print all cousins of given node. Note that siblings should not be printed.
Input : root of below tree 1 / \ 2 3 / \ / \ 4 5 6 7 and pointer to a node say 5. Output : 6, 7
Note that it is the same problem as given at Print cousins of a given node in Binary Tree which consists of two traversals recursively. In this post, a single level traversal approach is discussed.
The idea is to go for level order traversal of the tree, as the cousins and siblings of a node can be found in its level order traversal. Run the traversal till the level containing the node is not found, and if found, print the given level.
How to print the cousin nodes instead of siblings and how to get the nodes of that level in the queue? During the level order, when for the parent node, if parent->left == Node_to_find, or parent->right == Node_to_find, then the children of this parent must not be pushed into the queue (as one is the node and other will be its sibling). Push the remaining nodes at the same level in the queue and then exit the loop. The current queue will have the nodes at the next level (the level of the node being searched, except the node and its sibling). Now, print the queue.
Following is the implementation of the above algorithm.
Node not found Cousin Nodes : None Cousin Nodes : None Cousin Nodes : None Cousin Nodes : 6 7
Time Complexity : This is a single level order traversal, hence time complexity = O(n), and Auxiliary space = O(n) (See this).
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.
- Print cousins of a given node in Binary Tree
- Sum of cousins of a given node in a Binary Tree
- Check if two nodes are cousins in a Binary Tree
- Check if two nodes are cousins in a Binary Tree | Set-2
- Construct Full Binary Tree using its Preorder traversal and Preorder traversal of its mirror tree
- Level order traversal of Binary Tree using Morris Traversal
- Find parent of given node in a Binary Tree with given postorder traversal
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Deletion of a given node K in a Binary Tree using Level Order Traversal
- Zig Zag Level order traversal of a tree using single array
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Find n-th node in Preorder traversal of a Binary Tree
- Find n-th node in Postorder traversal of a Binary Tree
- Kth node in Diagonal Traversal of Binary Tree
- Create a binary tree from post order traversal and leaf node array
- Find the kth node in vertical order traversal of a Binary Tree
- Print a Binary Tree in Vertical Order | Set 3 (Using Level Order Traversal)
- Find the Kth node in the DFS traversal of a given subtree in a Tree
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- If you are given two traversal sequences, can you construct the 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.
Improved By : Rajput-Ji