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.
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.
- Largest value in each level of Binary Tree | Set-2 (Iterative Approach)
- Smallest value in each level of Binary Tree
- Difference between sums of odd level and even level nodes of a Binary Tree
- Count nodes from all lower levels smaller than minimum valued node of current level for every level in a Binary Tree
- Print nodes of a Binary Search Tree in Top Level Order and Reversed Bottom Level Order alternately
- Maximum value at each level in an N-ary Tree
- Print extreme nodes of each level of Binary Tree in alternate order
- Recursive Program to 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
- Difference between sums of odd level and even level nodes in an N-ary Tree
- Largest number possible by arranging node values at each level
- Queries to find the maximum Xor value between X and the nodes of a given level of a perfect binary tree
- Check if value exists in level-order sorted complete binary tree
- 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
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- General Tree (Each node can have arbitrary number of children) Level Order Traversal
- Connect Nodes at same Level (Level Order Traversal)
- Get Level of a node in a Binary Tree
- Print nodes between two given level numbers of 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 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.