Number of siblings of a given Node in n-ary Tree
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.