Given a Binary Tree and a Level. The task is to find the node with the maximum value at that given level.
The idea is to traverse the tree along depth recursively and return the nodes once the required level is reached and then return the maximum of left and right subtrees for each subsequent call. So that the last call will return the node with maximum value among all nodes at the given level.
Below is the step by step algorithm:
- Perform DFS traversal and every time decrease the value of level by 1 and keep traversing to the left and right subtrees recursively.
- When value of level becomes 0, it means we are on the given level, then return root->data.
- Find the maximum between the two values returned by left and right subtrees and return the maximum.
Below is the implementation of above approach:
It can also be done by using Queue, which uses level order traversal and it basically checks for the maximum node when the given level is equal to our count variable. (variable k).
Below is the implementation of the above approach:
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.
- Count nodes from all lower levels smaller than minimum valued node of current level for every level in a Binary Tree
- 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
- Print the nodes corresponding to the level value for each level of a Binary Tree
- Queries to find the maximum Xor value between X and the nodes of a given level of a perfect binary tree
- Print all the nodes except the leftmost node in every level of the given binary tree
- Deletion of a given node K in a Binary Tree using Level Order Traversal
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Find maximum level product in Binary Tree
- Find Maximum Level Sum in Binary Tree using Recursion
- Find maximum level sum in Binary Tree
- Difference between sums of odd level and even level nodes in an N-ary Tree
- Get Level of a node in a Binary Tree
- Depth of the deepest odd level node in Binary Tree
- 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 all nodes except rightmost node of every level of the Binary Tree
- Check if max sum level of Binary tree divides tree into two equal sum halves
- Find if given vertical level of binary tree is sorted or not
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.