VOOZH about

URL: https://www.geeksforgeeks.org/dsa/sum-leaf-nodes-minimum-level/

⇱ Sum of leaf nodes at minimum level - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Sum of leaf nodes at minimum level

Last Updated : 23 May, 2026

Given a Binary Tree, find the sum of all the leaf nodes that are at minimum level of the given binary tree.

Examples:

Input: 

👁 2056958142

Output: 13
Explanation:
The leaf nodes of the tree are 5, 7, 2 and 8.
Among these leaf nodes, the minimum level leaf nodes are 5 and 8.
So, the required sum = 5 + 8 = 13.

Input:

👁 2056958141

Output: 22
Explanation:
The leaf nodes of the tree are 4, 5, 6 and 7.
All the leaf nodes are present at the same minimum level.
So, the required sum = 4 + 5 + 6 + 7 = 22.

The idea is to perform a level order traversal of the binary tree using a queue. During traversal, we process nodes level by level and find the first level that contains a leaf node. Once this level is found, we sum all the leaf nodes present at this level and stop further traversal because this is the minimum level containing leaf nodes.

Let us understand with an example:

  • Start BFS traversal from root node 1. Queue becomes: [1]
  • Process level 0. Node 1 is not a leaf node, push its children 2 and 3 into the queue. Queue becomes: [2, 3]
  • Process level 1. Nodes 2 and 3 are not leaf nodes, so push their children 4, 5, 6 and 7. Queue becomes: [4, 5, 6, 7]
  • Process level 2. Nodes 4, 5, 6 and 7 are leaf nodes, so add them to sum. sum = 4 + 5 + 6 + 7 = 22
  • Since the first level containing leaf nodes is found, stop traversal and return: 22

Output
22

Time Complexity: O(n)
Space Complexity: O(n)

Comment