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), where N is the number of nodes in tree.
Auxiliary Space : O(N), where N is the number of nodes in tree.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Print siblings of a given Node in N-ary Tree
- Check if two nodes in a Binary Tree are siblings
- Find the maximum GCD of the siblings of a Binary Tree
- Queries to find sum of distance of a given node to every leaf node in a Weighted Tree
- Find farthest node from each node in Tree
- Count of all possible Paths in a Tree such that Node X does not appear before Node Y
- Farthest distance of a Node from each Node of a Tree
- Maximum XOR with given value in the path from root to given node in the tree
- Find parent of given node in a Binary Tree with given postorder traversal
- Number of children of given node in n-ary Tree
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Convert a Generic Tree(N-array Tree) to Binary Tree
- Print Ancestors of a given node in Binary Tree
- Print cousins of a given node in Binary Tree
- Closest leaf to a given node in Binary Tree
- Find distance from root to given node in a binary tree
- Print path from root to a given node in a binary tree
- Find mirror of a given node in Binary tree
- Find root of the tree where children id sum for every node is given
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.