Find the level in a binary tree which has the maximum number of nodes. The root is at level 0.
Input: Output : 2 Explanation: Input: Output:1 Explanation
Approach: It is known that in level order traversal of binary tree with queue, at any time our queue contains all elements of a particular level. So find level with maximum number of nodes in queue.
BFS traversal is an algorithm for traversing or searching tree or graphs . It starts at the tree root , and explores all of the neighbor nodes at the present depth prior to moving on to the nodes at the next depth level.
So at any point the queue of BFS will contain elements of adjacent layers. So this makes the algorithm perfect for this problem.
- Create the tree, a queue to store the nodes and insert the root in the queue. Create variables level=0,count =0 and level_no=-1
- The implementation will be slightly different, all the elements of same level will be removed in a single iteration.
- Run a loop while size of queue is greater than 0. Get the size of queue (size) and store it. If size is greater than count then update count = size and level_no = level.
- Now run a loop size times, and pop one node from the queue and insert its childrens (if present).
- Increment level.
Level having maximum number of nodes : 2
- Time Complexity : O(n).
In BFS traversal every node is visited only once, So Time Complexity is O(n).
- Space Complexity: O(n).
The space is required to store the nodes in a queue.
This article is contributed by Ayush Jauhari. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.
- Difference between sums of odd level and even level nodes of a Binary Tree
- Connect Nodes at same Level (Level Order Traversal)
- 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
- Print nodes of a Binary Search Tree in Top Level Order and Reversed Bottom Level Order alternately
- Level with maximum number of nodes using DFS in a N-ary tree
- Queries to find the maximum Xor value between X and the nodes of a given level of a perfect binary tree
- Count the number of nodes at given level in a tree using BFS.
- Print levels with odd number of nodes and even number of nodes
- Connect nodes at same level
- Connect nodes at same level using constant extra space
- Print nodes between two given level numbers of a binary tree
- Print extreme nodes of each level of Binary Tree in alternate order
- Swap Nodes in Binary tree of every k'th level
- Sum of nodes at k-th level in a tree represented as string
- Print odd positioned nodes of odd levels in level order of the given binary tree
- Sum of leaf nodes at minimum level
- Product of nodes at k-th level in a tree represented as string
- Print Leaf Nodes at a given Level
- Count nodes with two children at level L in a Binary Tree