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.
- 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
- Given a n-ary tree, count number of nodes which have more number of children than parents
- Number of full binary trees such that each node is product of its children
- 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 of nodes in a Binary Tree whose immediate children are co-prime
- 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
- Iterative approach to check for children sum property in a Binary Tree
- Number of siblings of a given Node in n-ary Tree
- Number of turns to reach from one node to other in binary 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
- Queries to find sum of distance of a given node to every leaf node in a Weighted Tree
- Farthest distance of a Node from each Node of a Tree
- Find farthest node from each node in Tree
- XOR of all the nodes in the sub-tree of the given node