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).
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Get level of a node in binary tree | iterative approach
- Check whether a binary tree is a full binary tree or not | Iterative Approach
- Check for Symmetric Binary Tree (Iterative Approach)
- Iterative approach to check if a Binary Tree is Perfect
- Sum of nodes at maximum depth of a Binary Tree | Iterative Approach
- Iterative approach to check for children sum property in a Binary Tree
- Deepest right leaf node in a binary tree | Iterative approach
- Construct Binary Tree from given Parent Array representation | Iterative Approach
- Print All Leaf Nodes of a Binary Tree from left to right | Set-2 ( Iterative Approach )
- Deepest left leaf node in a binary tree | iterative approach
- Largest value in each level of Binary Tree
- Iterative Approach to check if two Binary Trees are Isomorphic or not
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Difference between sums of odd level and even level nodes of a Binary Tree
- Check if max sum level of Binary tree divides tree into two equal sum halves
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Iterative Search for a key 'x' in Binary Tree
- Iterative diagonal traversal of binary tree
- Check whether a given Binary Tree is Complete or not | Set 1 (Iterative Solution)
- Iterative method to find ancestors of a given binary tree
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.