Given an n-ary tree, count number of ways to traverse an n-ary (or a Directed Acyclic Graph) tree starting from the root vertex.
Suppose we have a given N-ary tree as shown below.
Now we have to find the number of ways of traversing the whole tree starting from the root vertex. There can be many such ways. Some of them are listed below.
1) N->M->K->J->B->F->D->E->C->H->I->L->A (kind-of depth first traversal).
2) A->B->F->D->E->K->J->G->C->H->I->N->M->L (level order traversal)
and so on….
We strongly recommend you to minimize your browser and try this yourself first.
The count of all ways to traverse is the product of factorials of the number of children of each node. Refer to the below figure for clear understanding-
‘A’ has four children, so 4! permutations possible
‘B’ has two children, so 2! permutations possible
‘F’ has no children, so 0! permutations possible
And so on
Hence all such ways are- 4 ! * 2 ! * 0 ! * 1 ! * 3 ! * 2 ! * 0 ! * 0 ! * 0 ! * 0 ! * 1 ! * 0 ! * 0 ! = 576 way
That’s a huge number of ways and among them only few proves to be useful, like- inorder, level-order, preorder, postorder (arranged according to the popularity of these traversals)
Time Complexity: We visit each node once during the level order traversal and take O(n) time to compute factorial for every node. Total time taken is O(Nn) where N = number of nodes in the n-ary tree. We can optimize the solution to work in O(N) time by per-computing factorials of all numbers from 1 to n.
Auxiliary Space : Since we are only using a queue and a structure for every node, so overall space complexity is also O(N).
Common Pitfalls: Since, products of factorials can tend to grow very huge, so it may overflow. It is preferable to use data types like- unsigned long long int in C/C++, as the number of ways can never be a negative number. In Java and Python there are Big Integer to take care of overflows.
This article is contributed by Rachit Belwariar. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- Ways to color a skewed tree such that parent and child have different colors
- Given a n-ary tree, count number of nodes which have more number of children than parents
- Factor Tree of a given Number
- Number of children of given node in n-ary Tree
- Number of siblings of a given Node in n-ary Tree
- Number of Paths of Weight W in a K-ary tree
- Number of special nodes in an n-ary tree
- Number of nodes greater than a given value in n-ary tree
- Count the number of nodes at given level in a tree using BFS.
- Number of Isosceles triangles in a binary tree
- Level with maximum number of nodes using DFS in a N-ary tree
- Relationship between number of nodes and height of 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
- Number of turns to reach from one node to other in binary tree