Given a N-ary tree, the task is to print the level with the maximum number of nodes.
Input : For example, consider the following tree 1 - Level 1 / \ 2 3 - Level 2 / \ \ 4 5 6 - Level 3 / \ / 7 8 9 - Level 4 Output : Level-3 and Level-4
- Insert all the connecting nodes to a 2-D vector tree.
- Run a DFS on the tree such that height[node] = 1 + height[parent]
- Once DFS traversal is completed, increase the count array by 1, for every node’s level.
- Iterate from first level to last level, and find the level with the maximum number of nodes.
- Re-traverse from first to last level, and print all the levels which have the same number of maximum nodes.
Below is the implementation of the above approach.
The levels with maximum number of nodes are: 3 4
Time Complexity: O(N)
Auxiliary Space: O(N)
- Count the number of nodes at a given level in a tree using DFS
- Difference between sums of odd level and even level nodes of a Binary Tree
- Print nodes of a Binary Search Tree in Top Level Order and Reversed Bottom Level Order alternately
- Print the nodes corresponding to the level value for each level of a Binary Tree
- Count nodes from all lower levels smaller than minimum valued node of current level for every level in a Binary Tree
- Difference between sums of odd level and even level nodes in an N-ary Tree
- Subtree of all nodes in a tree using DFS
- Kth ancestor of all nodes in an N-ary tree using DFS
- Print all leaf nodes of an n-ary tree using DFS
- Print the path between any two nodes of a tree | DFS
- Calculate number of nodes in all subtrees | Using DFS
- Connect Nodes at same Level (Level Order Traversal)
- Calculate number of nodes between two vertices in an acyclic Graph by DFS method
- Count the number of nodes at given level in a tree using BFS.
- Queries to find the maximum Xor value between X and the nodes of a given level of a perfect binary tree
- Product of nodes at k-th level in a tree represented as string using Recursion
- Diameter of a tree using DFS
- DFS traversal of a tree using recursion
- Construct the Rooted tree by using start and finish time of its DFS traversal
- Level with maximum number of nodes
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.