Given a binary tree containing n nodes. The problem is to find and print the largest value present in each level.
Input : 1 / \ 2 3 Output : 1 3 Input : 4 / \ 9 2 / \ \ 3 5 7 Output : 4 9 7
Approach: In the previous post, a recursive method have been discussed. In this post an iterative method has been discussed. The idea is to perform iterative level order traversal of the binary tree using queue. While traversing keep max variable which stores the maximum element of the current level of the tree being processed. When the level is completely traversed, print that max value.
4 9 7
Time Complexity: O(n).
Auxiliary Space: O(n).
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.
- Get level of a node in binary tree | iterative approach
- Check whether a binary tree is a full binary tree or not | Iterative Approach
- Largest value in each level of Binary Tree
- Smallest value in each level of Binary Tree
- Deepest left leaf node in a binary tree | iterative approach
- Sum of nodes at maximum depth of a Binary Tree | Iterative Approach
- Iterative approach to check for children sum property in a Binary Tree
- Iterative Approach to check if two Binary Trees are Isomorphic or not
- Print extreme nodes of each level of Binary Tree in alternate order
- Difference between sums of odd position and even position nodes for each level of a Binary Tree
- Largest number possible by arranging node values at each level
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Check if max sum level of Binary tree divides tree into two equal sum halves
- General Tree (Each node can have arbitrary number of children) Level Order Traversal
- Iterative diagonal traversal of binary tree
- Iterative Method To Print Left View of a Binary Tree
- Connect Nodes at same Level (Level Order Traversal)
- Print nodes between two given level numbers of a binary tree
- Perfect Binary Tree Specific Level Order Traversal
- Print a Binary Tree in Vertical Order | Set 3 (Using Level Order Traversal)
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.