Check if two nodes in a Binary Tree are siblings

Given a binary tree and two nodes, the task is to check if the nodes are siblings of each other or not.

Two nodes are said to be siblings if they are present at the same level, and their parents are same.

Examples:

Input : 
       1
      /  \
     2    3
    / \  / \
   4   5 6  7
First node is 4 and Second node is 6.
Output : No, they are not siblings.

Input :
         1
        /  \
       5    6
      /    /  \
     7     3   4
First node is 3 and Second node is 4
Output : Yes

Approach: On observing carefully, it can be concluded that any node in a binary tree can have maximum of two child nodes. So, since the parent of two siblings must be same, so the idea is to simply traverse the tree and for every node check if the two given nodes are it’s children. If it is true for any node in the tree then print YES otherwise print NO.

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to check if two nodes are
// siblings
  
#include <bits/stdc++.h>
using namespace std;
  
// Binary Tree Node
struct Node {
    int data;
    Node *left, *right;
};
  
// Utility function to create a new node
struct Node* newNode(int data)
{
    struct Node* node = new Node;
    node->data = data;
    node->left = node->right = NULL;
  
    return (node);
}
  
// Function to find out if two nodes are siblings
bool CheckIfNodesAreSiblings(Node* root, int data_one,
                             int data_two)
{
    if (!root)
        return false;
  
    // Compare the two given nodes with
    // the childrens of current node
    if (root->left && root->right) {
        int left = root->left->data;
        int right = root->right->data;
  
        if (left == data_one && right == data_two)
            return true;
        else if (left == data_two && right == data_one)
            return true;
    }
  
    // Check for left subtree
    if (root->left)
        CheckIfNodesAreSiblings(root->left, data_one,
                                data_two);
  
    // Check for right subtree
    if (root->right)
        CheckIfNodesAreSiblings(root->right, data_one,
                                data_two);
}
  
// Driver code
int main()
{
  
    struct Node* root = newNode(1);
    root->left = newNode(2);
    root->right = newNode(3);
    root->left->left = newNode(4);
    root->right->left = newNode(5);
    root->right->right = newNode(6);
    root->left->left->right = newNode(7);
  
    int data_one = 5;
    int data_two = 6;
  
    if (CheckIfNodesAreSiblings(root, data_one, data_two))
        cout << "YES";
    else
        cout << "NO";
  
    return 0;
}

chevron_right


Output:

YES


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.