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)
- Queries to find the maximum Xor value between X and the nodes of a given level of a perfect binary tree
- Level with maximum number of nodes
- Count the number of nodes at a given level in a tree using DFS
- Count the number of nodes at given level in a tree using BFS.
- Difference between sums of odd level and even level nodes of a Binary Tree
- Swap Nodes in Binary tree of every k'th level
- Sum of nodes at k-th level in a tree represented as string
- Print nodes between two given level numbers of a binary tree
- Count nodes with two children at level L in a Binary Tree
- Product of nodes at k-th level in a tree represented as string
- Print all the nodes except the leftmost node in every level of the given binary tree
- Print odd positioned nodes of odd levels in level order of the given binary tree
- Print even positioned nodes of odd levels in level order of the given binary tree
- Print even positioned nodes of even levels in level order of the given binary tree
- Print extreme nodes of each level of Binary Tree in alternate order
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.
Improved By : rituraj_jain