Print cousins of a given node in Binary Tree | Single Traversal
Given a binary tree and a node, print all cousins of given node. Note that siblings should not be printed.
Examples:
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.
C++
// CPP program to print cousins of a node #include <iostream> #include <queue> using namespace std; // A Binary Tree Node struct Node { int data; Node *left, *right; }; // A utility function to create a new Binary // Tree Node Node* newNode(int item) { Node* temp = new Node; temp->data = item; temp->left = temp->right = NULL; return temp; } // function to print cousins of the node void printCousins(Node* root, Node* node_to_find) { // if the given node is the root itself, // then no nodes would be printed if (root == node_to_find) { cout << "Cousin Nodes : None" << endl; return; } queue<Node*> q; bool found = false; int size_; Node* p; q.push(root); // the following loop runs until found is // not true, or q is not empty. // if found has become true => we have found // the level in which the node is present // and the present queue will contain all the // cousins of that node while (!q.empty() && !found) { size_ = q.size(); while (size_) { p = q.front(); q.pop(); // if current node's left or right child // is the same as the node to find, // then make found = true, and don't push // any of them into the queue, as // we don't have to print the siblings if ((p->left == node_to_find || p->right == node_to_find)) { found = true; } else { if (p->left) q.push(p->left); if (p->right) q.push(p->right); } size_--; } } // if found == true then the queue will contain the // cousins of the given node if (found) { cout << "Cousin Nodes : "; size_ = q.size(); // size_ will be 0 when the node was at the // level just below the root node. if (size_ == 0) cout << "None"; for (int i = 0; i < size_; i++) { p = q.front(); q.pop(); cout << p->data << " "; } } else { cout << "Node not found"; } cout << endl; return; } // Driver Program to test above function int main() { Node* root = newNode(1); root->left = newNode(2); root->right = newNode(3); root->left->left = newNode(4); root->left->right = newNode(5); root->left->right->right = newNode(15); root->right->left = newNode(6); root->right->right = newNode(7); root->right->left->right = newNode(8); Node* x = newNode(43); printCousins(root, x); printCousins(root, root); printCousins(root, root->right); printCousins(root, root->left); printCousins(root, root->left->right); return 0; } |
Java
// Java program to print // cousins of a node import java.io.*; import java.util.*; import java.lang.*; // A Binary Tree Node class Node { int data; Node left, right; Node(int key) { data = key; left = right = null; } } class GFG { // function to print // cousins of the node static void printCousins(Node root, Node node_to_find) { // if the given node // is the root itself, // then no nodes would // be printed if (root == node_to_find) { System.out.print("Cousin Nodes :" + " None" + "\n"); return; } Queue<Node> q = new LinkedList<Node>(); boolean found = false; int size_ = 0; Node p = null; q.add(root); // the following loop runs // until found is not true, // or q is not empty. if // found has become true => we // have found the level in // which the node is present // and the present queue will // contain all the cousins of // that node while (q.isEmpty() == false && found == false) { size_ = q.size(); while (size_ -- > 0) { p = q.peek(); q.remove(); // if current node's left // or right child is the // same as the node to find, // then make found = true, // and don't push any of them // into the queue, as we don't // have to print the siblings if ((p.left == node_to_find || p.right == node_to_find)) { found = true; } else { if (p.left != null) q.add(p.left); if (p.right!= null) q.add(p.right); } } } // if found == true then the // queue will contain the // cousins of the given node if (found == true) { System.out.print("Cousin Nodes : "); size_ = q.size(); // size_ will be 0 when // the node was at the // level just below the // root node. if (size_ == 0) System.out.print("None"); for (int i = 0; i < size_; i++) { p = q.peek(); q.poll(); System.out.print(p.data + " "); } } else { System.out.print("Node not found"); } System.out.println(""); return; } // Driver code public static void main(String[] args) { Node root = new Node(1); root.left = new Node(2); root.right = new Node(3); root.left.left = new Node(4); root.left.right = new Node(5); root.left.right.right = new Node(15); root.right.left = new Node(6); root.right.right = new Node(7); root.right.left.right = new Node(8); Node x = new Node(43); printCousins(root, x); printCousins(root, root); printCousins(root, root.right); printCousins(root, root.left); printCousins(root, root.left.right); } } |
C#
// C# program to print // cousins of a node using System; using System.Collections.Generic; // A Binary Tree Node public class Node { public int data; public Node left, right; public Node(int key) { data = key; left = right = null; } } public class GFG { // function to print // cousins of the node static void printCousins(Node root, Node node_to_find) { // if the given node // is the root itself, // then no nodes would // be printed if (root == node_to_find) { Console.Write("Cousin Nodes :" + " None" + "\n"); return; } Queue<Node> q = new Queue<Node>(); bool found = false; int size_ = 0; Node p = null; q.Enqueue(root); // the following loop runs // until found is not true, // or q is not empty. if // found has become true => we // have found the level in // which the node is present // and the present queue will // contain all the cousins of // that node while (q.Count!=0 && found == false) { size_ = q.Count; while (size_ -- > 0) { p = q.Peek(); q.Dequeue(); // if current node's left // or right child is the // same as the node to find, // then make found = true, // and don't push any of them // into the queue, as we don't // have to print the siblings if ((p.left == node_to_find || p.right == node_to_find)) { found = true; } else { if (p.left != null) q.Enqueue(p.left); if (p.right!= null) q.Enqueue(p.right); } } } // if found == true then the // queue will contain the // cousins of the given node if (found == true) { Console.Write("Cousin Nodes : "); size_ = q.Count; // size_ will be 0 when // the node was at the // level just below the // root node. if (size_ == 0) Console.Write("None"); for (int i = 0; i < size_; i++) { p = q.Peek(); q.Dequeue(); Console.Write(p.data + " "); } } else { Console.Write("Node not found"); } Console.WriteLine(""); return; } // Driver code public static void Main(String[] args) { Node root = new Node(1); root.left = new Node(2); root.right = new Node(3); root.left.left = new Node(4); root.left.right = new Node(5); root.left.right.right = new Node(15); root.right.left = new Node(6); root.right.right = new Node(7); root.right.left.right = new Node(8); Node x = new Node(43); printCousins(root, x); printCousins(root, root); printCousins(root, root.right); printCousins(root, root.left); printCousins(root, root.left.right); } } // This code is contributed Rajput-Ji |
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).
Recommended Posts:
- Print cousins of a given node in Binary Tree
- Sum of cousins of a given node in a Binary Tree
- Kth node in Diagonal Traversal of Binary Tree
- Find n-th node in Preorder traversal of a Binary Tree
- Find n-th node in Postorder traversal of a Binary Tree
- Find the kth node in vertical order traversal of a Binary Tree
- Print Ancestors of a given node in Binary Tree
- Print the nodes of binary tree as they become the leaf node
- Print the number of set bits in each node of a Binary Tree
- Print path from root to a given node in a binary tree
- Check if two nodes are cousins in a Binary Tree | Set-2
- Check if two nodes are cousins in a Binary Tree
- Print all the nodes except the leftmost node in every level of the given binary tree
- Zig Zag Level order traversal of a tree using single queue
- Print a Binary Tree in Vertical Order | Set 3 (Using Level Order Traversal)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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



