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.
- Connect Nodes at same Level (Level Order Traversal)
- Print nodes between two given level numbers of a binary tree
- Print extreme nodes of each level of Binary Tree in alternate order
- Print odd positioned nodes of odd levels in level order of the given binary tree
- Sum of leaf nodes at minimum level
- Print even positioned nodes of even levels in level order of the given binary tree
- Difference between sums of odd position and even position nodes for each level of a Binary Tree
- Print all nodes except rightmost node of every level of the Binary Tree
- Sum of all nodes at Kth level in a Binary Tree
- Find maximum level sum in Binary Tree
- Find maximum level product in Binary Tree
- Find the sum of leafs at maximum level
- Maximum absolute difference between any two level sum in a Binary Tree
- Maximum number of leaf nodes that can be visited within the given budget
- General Tree (Each node can have arbitrary number of children) Level Order Traversal
- Largest number possible by arranging node values at each level
- Sum of nodes at maximum depth of a Binary Tree | Iterative Approach
- Maximum sum of leaf nodes among all levels of the given binary tree
- Maximum sum of non-leaf nodes among all levels of the given binary tree
- Level Order Tree Traversal