Given a node x, find the number of children of x(if it exists) in the given n-ary tree.
Input : x = 50 Output : 3 Explanation : 50 has 3 children having values 40, 100 and 20.
- Initialize the number of children as 0.
- For every node in the n-ary tree, check if its value is equal to x or not. If yes, then return the number of children.
- If the value of x is not equal to the current node then, push all the children of current node in the queue.
- Keep Repeating the above step until the queue becomes empty.
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.
- Find root of the tree where children id sum for every node is given
- General Tree (Each node can have arbitrary number of children) Level Order Traversal
- Given a n-ary tree, count number of nodes which have more number of children than parents
- Node having maximum sum of immediate children and itself in n-ary tree
- Convert an arbitrary Binary Tree to a tree that holds Children Sum Property
- Number of full binary trees such that each node is product of its children
- Check for Children Sum Property in a Binary Tree
- Maximum parent children sum in Binary tree
- Iterative approach to check for children sum property in a Binary Tree
- Count of nodes in a Binary tree with immediate children as its factors
- Count nodes with two children at level L in a Binary Tree
- Count of nodes in a Binary Tree whose immediate children are co-prime
- Count of Nodes whose both immediate children are its prime factors
- 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 siblings of a given Node in n-ary 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.