Given a binary tree, find the largest value in each level.
Input : 1 / \ 2 3 Output : 1 3 Input : 4 / \ 9 2 / \ \ 3 5 7 Output : 4 9 7
Approach : The idea is to recursively traverse tree in a pre-order fashion. Root is considered to be at zeroth level. While traversing, keep track of the level of the element and if its current level is not equal to the number of elements present in the list, update the maximum element at that level in the list.
Below is the implementation to find largest value on each level of Binary Tree.
4 9 7
- Time complexity : O(n), where n is the number of nodes in binary tree.
- Auxiliary Space : O(n) as in worst case, depth of binary tree will be n.
- Largest value in each level of Binary Tree | Set-2 (Iterative Approach)
- Difference between sums of odd level and even level nodes of a Binary Tree
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Get Level of a node in a Binary Tree
- Smallest value in each level of Binary Tree
- Height of binary tree considering even level leaves only
- Find maximum level sum in Binary Tree
- Swap Nodes in Binary tree of every k'th level
- Insertion in a Binary Tree in level order
- Density of Binary Tree using Level Order Traversal
- Get level of a node in binary tree | iterative approach
- Print nodes between two given level numbers of a binary tree
- Find maximum level product in Binary Tree
- Find the maximum node at a given level in a binary tree
- Count nodes with two children at level L in a 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.
Improved By : SHUBHAMSINGH10