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
- Insertion in a Binary Tree in level order
- Swap Nodes in Binary tree of every k'th level
- Find maximum level sum in Binary Tree
- Density of Binary Tree using Level Order Traversal
- Get level of a node in binary tree | iterative approach
- Level Order Predecessor of a node in Binary Tree
- Level Order Successor of a node in Binary Tree
- Print nodes between two given level numbers of a binary tree
- Depth of the deepest odd level node in 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 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.