VOOZH about

URL: https://www.geeksforgeeks.org/dsa/largest-value-level-binary-tree/

⇱ Largest value in each level of Binary Tree - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Largest value in each level of Binary Tree

Last Updated : 22 May, 2024

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
Recommended Problem

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:


Output
4 9 7 

Largest value in each level of Binary Tree | Set-2 (Iterative Approach)

Complexity Analysis:

  • 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.

Another Approach:

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:


Output
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:

  1. We start by initializing an empty vector res to store the largest values on each level of the tree.
  2. We perform a BFS traversal of the binary tree using a queue. We start by pushing the root node into the queue.
  3. While the queue is not empty, we process each level of the tree:                                                                                                             a. Get the current size of the queue. This represents the number of nodes at the current level.                                                                 b. Initialize a variable levelMax to store the maximum value at the current level. Set it to the minimum possible integer value (INT_MIN).                                                                                                                                                                                                 c. Iterate through all the nodes at the current level. Remove each node from the queue and update levelMax if the value of the current node is greater than levelMax.                                                                                                                                                                   d. Enqueue the left and right child nodes of the current node (if they exist) to continue the BFS traversal of the next level.                   e. Once we process all the nodes at the current level, we have the maximum value for that level. Append levelMax to the res vector.
  4. After the BFS traversal is complete, the res vector will contain the largest value on each level of the binary tree.
  5. Finally, we return the res vector.

Output
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.     

Comment
Article Tags: