![]() |
VOOZH | about |
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:
Below is the implementation of above approach:
49
Complexity Analysis:
Iterative 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:
49
Complexity Analysis: