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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- General Tree (Each node can have arbitrary number of children) Level Order Traversal
- Node having maximum sum of immediate children and itself in n-ary tree
- Find root of the tree where children id sum for every node is given
- Number of full binary trees such that each node is product of its children
- Given a n-ary tree, count number of nodes which have more number of children than parents
- Convert an arbitrary Binary Tree to a tree that holds Children Sum Property
- Check for Children Sum Property in a Binary Tree
- Maximum parent children sum in Binary tree
- Count nodes with two children at level L in a Binary Tree
- Count of nodes in a Binary tree with immediate children as its factors
- Count of nodes in a Binary Tree whose immediate children are co-prime
- Number of siblings of a given Node in n-ary Tree
- Iterative approach to check for children sum property in a Binary Tree
- Print the number of set bits in each node of a Binary Tree
- Number of turns to reach from one node to other in binary tree
- Number of leaf nodes in the subtree of every node of an n-ary tree
- Find farthest node from each node in Tree
- XOR of all the nodes in the sub-tree of the given node
- Get Level of a node in a Binary Tree
- K-th ancestor of a node in 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 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.