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:
// 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; } |
YES
Recommended Posts:
- Check if two nodes are cousins in a Binary Tree
- Check if two nodes are cousins in a Binary Tree | Set-2
- Check sum of Covered and Uncovered nodes of Binary Tree
- Check whether a binary tree is a full binary tree or not | Iterative Approach
- Number of siblings of a given Node in n-ary Tree
- Sum of all nodes in a binary tree
- Sink Odd nodes in Binary Tree
- Sum of all leaf nodes of binary tree
- Product of all nodes in a Binary Tree
- XOR of path between any two nodes in a Binary Tree
- Sum of nodes in top view of binary tree
- Sum of all the Boundary Nodes of a Binary Tree
- Count Non-Leaf nodes in a Binary Tree
- Maximum sum of nodes in Binary tree such that no two are adjacent
- Print all nodes in a binary tree having K leaves
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.



