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.

Try It Yourself
redirect icon

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
C++
#include <bits/stdc++.h>
using namespace std;

class Node
{
  public:
    int data;
    Node *left;
    Node *right;

    Node(int val)
    {
        data = val;
        left = right = nullptr;
    }
};

// Function to find the sum of
// leaf nodes at minimum level
int minLeafSum(Node *root)
{

    // If tree is empty
    if (root == nullptr)
        return 0;

    // If root itself is a leaf node
    if (root->left == nullptr && root->right == nullptr)
        return root->data;

    // Queue for level order traversal
    queue<Node *> q;

    q.push(root);

    int sum = 0;
    bool found = false;

    while (!q.empty() && !found)
    {

        // Number of nodes at current level
        int size = q.size();

        while (size--)
        {

            Node *curr = q.front();
            q.pop();

            // Check if current node is a leaf node
            if (curr->left == nullptr && curr->right == nullptr)
            {

                sum += curr->data;
                found = true;
            }

            else
            {

                // Push left child
                if (curr->left)
                    q.push(curr->left);

                // Push right child
                if (curr->right)
                    q.push(curr->right);
            }
        }
    }

    return sum;
}

// Driver code
int main()
{

    Node *root = new Node(1);

    root->left = new Node(2);
    root->right = new Node(3);

    root->left->left = new Node(4);
    root->left->right = new Node(5);

    root->right->left = new Node(6);
    root->right->right = new Node(7);

    cout << minLeafSum(root);

    return 0;
}
Java
import java.util.Queue;
import java.util.LinkedList;

class Node {
  public int data;
  public Node left;
  public Node right;

  public Node(int val) {
    data = val;
    left = right = null;
  }
}

public class GfG {
  // Function to find the sum of
  // leaf nodes at minimum level
  public static int minLeafSum(Node root) {
    // If tree is empty
    if (root == null)
      return 0;

    // If root itself is a leaf node
    if (root.left == null && root.right == null)
      return root.data;

    // Queue for level order traversal
    Queue<Node> q = new LinkedList<>();

    q.add(root);

    int sum = 0;
    boolean found = false;

    while (!q.isEmpty() &&!found) {
      // Number of nodes at current level
      int size = q.size();

      while (size-- > 0) {
        Node curr = q.poll();

        // Check if current node is a leaf node
        if (curr.left == null && curr.right == null) {
          sum += curr.data;
          found = true;
        } else {
          // Push left child
          if (curr.left!= null)
            q.add(curr.left);

          // Push right child
          if (curr.right!= null)
            q.add(curr.right);
        }
      }
    }

    return sum;
  }

  public static void main(String[] args) {
    Node root = new Node(1);

    root.left = new Node(2);
    root.right = new Node(3);

    root.left.left = new Node(4);
    root.left.right = new Node(5);

    root.right.left = new Node(6);
    root.right.right = new Node(7);

    System.out.println(minLeafSum(root));
  }
}
Python
from collections import deque

class Node:
  def __init__(self, val):
    self.data = val
    self.left = None
    self.right = None

# Function to find the sum of
# leaf nodes at minimum level
def minLeafSum(root):
  # If tree is empty
  if root is None:
    return 0

  # If root itself is a leaf node
  if root.left is None and root.right is None:
    return root.data

  # Queue for level order traversal
  q = deque([root])

  sum = 0
  found = False

  while q and not found:
    # Number of nodes at current level
    size = len(q)

    while size > 0:
      curr = q.popleft()

      # Check if current node is a leaf node
      if curr.left is None and curr.right is None:
        sum += curr.data
        found = True
      else:
        # Push left child
        if curr.left is not None:
          q.append(curr.left)
        # Push right child
        if curr.right is not None:
          q.append(curr.right)
      size -= 1

  return sum

# Driver code
if __name__ == '__main__':
  root = Node(1)

  root.left = Node(2)
  root.right = Node(3)

  root.left.left = Node(4)
  root.left.right = Node(5)

  root.right.left = Node(6)
  root.right.right = Node(7)

  print(minLeafSum(root))
C#
using System;
using System.Collections.Generic;

public class Node
{
  public int data;
  public Node left;
  public Node right;

  public Node(int val)
  {
    data = val;
    left = right = null;
  }
}

public class GfG
{
  // Function to find the sum of
  // leaf nodes at minimum level
  public static int minLeafSum(Node root)
  {
    // If tree is empty
    if (root == null)
      return 0;

    // If root itself is a leaf node
    if (root.left == null && root.right == null)
      return root.data;

    // Queue for level order traversal
    Queue<Node> q = new Queue<Node>();

    q.Enqueue(root);

    int sum = 0;
    bool found = false;

    while (q.Count > 0 &&!found)
    {
      // Number of nodes at current level
      int size = q.Count;

      while (size-- > 0)
      {
        Node curr = q.Dequeue();

        // Check if current node is a leaf node
        if (curr.left == null && curr.right == null)
        {
          sum += curr.data;
          found = true;
        }
        else
        {
          // Push left child
          if (curr.left!= null)
            q.Enqueue(curr.left);

          // Push right child
          if (curr.right!= null)
            q.Enqueue(curr.right);
        }
      }
    }

    return sum;
  }

  public static void Main()
  {
    Node root = new Node(1);

    root.left = new Node(2);
    root.right = new Node(3);

    root.left.left = new Node(4);
    root.left.right = new Node(5);

    root.right.left = new Node(6);
    root.right.right = new Node(7);

    Console.WriteLine(minLeafSum(root));
  }
}
JavaScript
class Node {
  constructor(val) {
    this.data = val;
    this.left = null;
    this.right = null;
  }
}

// Function to find the sum of
// leaf nodes at minimum level
function minLeafSum(root) {
  // If tree is empty
  if (root === null)
    return 0;

  // If root itself is a leaf node
  if (root.left === null && root.right === null)
    return root.data;

  // Queue for level order traversal
  let q = [root];

  let sum = 0;
  let found = false;

  while (q.length > 0 &&!found) {
    // Number of nodes at current level
    let size = q.length;

    while (size-- > 0) {
      let curr = q.shift();

      // Check if current node is a leaf node
      if (curr.left === null && curr.right === null) {
        sum += curr.data;
        found = true;
      } else {
        // Push left child
        if (curr.left!== null)
          q.push(curr.left);

        // Push right child
        if (curr.right!== null)
          q.push(curr.right);
      }
    }
  }

  return sum;
}

// Driver code
let root = new Node(1);

root.left = new Node(2);
root.right = new Node(3);

root.left.left = new Node(4);
root.left.right = new Node(5);

root.right.left = new Node(6);
root.right.right = new Node(7);

console.log(minLeafSum(root));

Output
22

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

Comment