Check if two nodes are cousins in a Binary Tree | Set-2

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.


   /   \
  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:

// CPP program to check if two Nodes in
// a binary tree are cousins
// using level-order traversals
#include <bits/stdc++.h>
using namespace std;

// A Binary Tree Node
struct Node {
    int data;
    struct Node *left, *right;

// A utility function to create a new
// Binary Tree Node
struct Node* newNode(int item)
    struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
    temp->data = item;
    temp->left = temp->right = NULL;
    return temp;

// Returns true if a and b are cousins,
// otherwise false.
bool isCousin(Node* root, Node* a, Node* b)
    if (root == NULL)
        return false;

    // To store parent of node a.
    Node* parA = NULL;

    // To store parent of node b.
    Node* parB = NULL;

    // queue to perform level order
    // traversal. Each element of
    // queue is a pair of node and
    // its parent.
    queue<pair<Node*, Node*> > q;

    // Dummy node to act like parent
    // of root node.
    Node* tmp = newNode(-1);

    // To store front element of queue.
    pair<Node*, Node*> ele;

    // Push root to queue.
    q.push(make_pair(root, tmp));
    int levSize;

    while (!q.empty()) {

        // find number of elements in
        // current level.
        levSize = q.size();
        while (levSize) {

            ele = q.front();

            // check if current node is node a
            // or node b or not.
            if (ele.first->data == a->data) {
                parA = ele.second;

            if (ele.first->data == b->data) {
                parB = ele.second;

            // push children of current node
            // to queue.
            if (ele.first->left) {
                q.push(make_pair(ele.first->left, ele.first));

            if (ele.first->right) {
                q.push(make_pair(ele.first->right, ele.first));


            // If both nodes are found in
            // current level then no need
            // to traverse current level further.
            if (parA && parB)

        // Check if both nodes are siblings
        // or not.
        if (parA && parB) {
            return parA != parB;

        // If one node is found in current level
        // and another is not found, then
        // both nodes are not cousins.
        if ((parA && !parB) || (parB && !parA)) {
            return false;

    return false;
// Driver Code
int main()
           /  \ 
          2    3
         / \  / \
        4   5 6  7
             \ \
             15 8

    struct 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);

    struct Node *Node1, *Node2;
    Node1 = root->left->left;
    Node2 = root->right->right;

    isCousin(root, Node1, Node2) ? puts("Yes") : puts("No");

    return 0;


Time Complexity: O(n)
Auxiliary Space: O(n)

A Programmer and A Machine learning Enthusiast

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Practice Tags :

Recommended Posts:

2.6 Average Difficulty : 2.6/5.0
Based on 3 vote(s)