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) where N is the total number of nodes in the tree.
In level order traversal, every node of the tree is processed once and hence the complexity due to the level order traversal is O(N) if there are total N nodes in the tree. Also, while processing every node, we are maintaining the maximum element at each level, however, this does not affect the overall time complexity. Therefore, the time complexity is O(N).
Auxiliary Space : O(w) where w is the maximum width of the tree.
In level order traversal, a queue is maintained whose maximum size at any moment can go upto the maximum width of the binary tree.
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
- Print the nodes corresponding to the level value for each level of a Binary Tree
- Check whether a binary tree is a full binary tree or not | Iterative Approach
- Largest value in each level of Binary Tree
- Check for Symmetric Binary Tree (Iterative Approach)
- Deepest left leaf node in a binary tree | iterative approach
- Deepest right leaf node in a binary tree | Iterative approach
- Print All Leaf Nodes of a Binary Tree from left to right | Set-2 ( Iterative Approach )
- Construct Binary Tree from given Parent Array representation | Iterative Approach
- Iterative approach to check if a Binary Tree is Perfect
- Iterative approach to check for children sum property in a Binary Tree
- Sum of nodes at maximum depth of a Binary Tree | Iterative Approach
- Smallest value in each level of Binary Tree
- 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
- Print nodes of a Binary Search Tree in Top Level Order and Reversed Bottom Level Order alternately
- Count nodes from all lower levels smaller than minimum valued node of current level for every level in a Binary Tree
- Iterative Approach to check if two Binary Trees are Isomorphic or not
- Maximum value at each level in an N-ary 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.