Given an N-ary tree, find the number of siblings of given node x. Assume that x exists in the given n-ary tree.
Input : 30 Output : 3
Approach : For every node in the given n-ary tree, push the children of the current node in the queue. While adding the children of current node in queue, check if any children is equal to the given value x or not. If yes, then return the number of siblings of x.
Below is the implementation of the above idea :
Time Complexity : O(N^2), where N is the number of nodes in tree.
Auxiliary Space : O(N), where N is the number of nodes in tree.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Find the maximum GCD of the siblings of a Binary Tree
- Check if two nodes in a Binary Tree are siblings
- Number of children of given node in n-ary Tree
- Print the number of set bits in each node of a Binary Tree
- Number of leaf nodes in the subtree of every node of an n-ary tree
- Number of turns to reach from one node to other in binary tree
- General Tree (Each node can have arbitrary number of children) Level Order Traversal
- Find farthest node from each node in Tree
- XOR of all the nodes in the sub-tree of the given node
- K-th ancestor of a node in Binary Tree | Set 3
- Search a node in Binary Tree
- K-th ancestor of a node in Binary Tree
- Node having maximum sum of immediate children and itself in n-ary tree
- Get Level of a node in a Binary Tree
- Kth ancestor of a node in binary tree | Set 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.