![]() |
VOOZH | about |
Given a binary tree, find the largest value in each level.
Examples :
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.
Implementation:
4 9 7
Largest value in each level of Binary Tree | Set-2 (Iterative Approach)
Complexity Analysis:
The above approach for finding the largest value on each level of a binary tree is based on the level-order traversal technique.
Intuition:
The approach first creates an empty queue and pushes the root node into it. Then it enters into a while loop that will run until the queue is not empty. Inside the while loop, it first calculates the current size of the queue, which represents the number of nodes present at the current level. Then it loops through all the nodes at the current level and pushes their child nodes (if any) into the queue. While doing this, it also checks if the current node's value is greater than the current maximum value for that level. If it is greater, then it updates the maximum value for that level.
After the loop, it adds the current maximum value to the result vector. Once all the levels are traversed, the result vector containing the largest value on each level is returned.
This approach has a time complexity of O(N), where N is the number of nodes in the binary tree, as it traverses each node only once. The space complexity of this approach is also O(N), as the maximum number of nodes that can be present in the queue at any given time is N/2 (the number of nodes in the last level of a complete binary tree).
Here are the implementation:
4 9 7
Time complexity: O(N)
Auxiliary Space: O(W)
The time complexity of the above approach is O(N), where N is the number of nodes in the binary tree. This is because the algorithm visits each node once, and each operation performed on a node takes constant time.
The space complexity of the algorithm is O(W), where W is the maximum width of the binary tree. In the worst case, the algorithm will have to store all the nodes in the last level of the binary tree in the queue before processing them. The maximum number of nodes that can be present in the last level of a binary tree is (N+1)/2, where N is the total number of nodes in the tree. Therefore, the space complexity of the algorithm is O((N+1)/2), which simplifies to O(N) in the worst case.
This program using a Breadth-First Search (BFS) approach finds the largest value on each level of a binary tree. Here's the intuition behind the algorithm:
4 9 7
Time complexity: O(N), where N is the number of nodes in the binary tree, as we need to visit each node once.
Auxiliary Space: O(M), where M is the maximum number of nodes at any level in the tree, as the queue can store at most M nodes at a time during the BFS traversal.