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.
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:
- Check if two nodes are cousins in a Binary Tree | Set-2
- Check if two nodes are cousins in a Binary Tree
- 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
- Sum of nodes in top view of binary tree
- Sum of all the Boundary Nodes of a Binary Tree
- Sink Odd nodes in Binary Tree
- Product of all nodes in a Binary Tree
- XOR of path between any two nodes in a Binary Tree
- Sum of all leaf nodes of binary tree
- Print path between any two nodes in a Binary Tree
- Given a binary tree, how do you remove all the half nodes?
- Print all internal nodes of a 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 firstname.lastname@example.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.