Sum of all left leaves in a given Binary Tree

Last Updated : 28 Aug, 2026

Given a Binary Tree, return the sum of all left leaf nodes. A left leaf node is a node that is the left child of its parent and does not have any left or right child.

Examples:

Input: root = [3, 1, 2]

211

Output: 1
Explanation: The leaf nodes of the tree are 1 and 2. The only left leaf node is 1. Hence, the required sum is 1.

Input: root = [1, 2, 3, 4, 5, N, 8, 7, 2, N, N, 6, 9]

2484

Output: 13
Explanation: The leaf nodes of the tree are 7, 2, 5, 6, and 9. The left leaf nodes are 7 and 6. Therefore, the required sum is: 7 + 6 = 13.

Try It Yourself
redirect icon

Using Recursive DFS - O(n) Time and O(n) Space

The idea is to traverse the tree recursively and check whether the left child of the current node is a leaf. If it is, add its value to the sum.

  • If the tree is empty, return 0.
  • Check whether the current node's left child exists and is a leaf.
  • If it is a left leaf, add its value to the sum.
  • Otherwise, recursively process the left subtree.
  • Recursively process the right subtree.
  • Return the total sum of all left leaf nodes.
C++
#include <bits/stdc++.h>
using namespace std;

// Definition for a binary tree node.
class Node
{
  public:
    int data;
    Node *left;
    Node *right;

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

// Check whether the given node is a leaf.
bool isLeaf(Node *node)
{
    return node != nullptr && node->left == nullptr && node->right == nullptr;
}

int leftLeavesSum(Node *root)
{
    // Base case: empty tree.
    if (root == nullptr)
        return 0;

    int sum = 0;

    // If the left child is a leaf, add its value.
    if (isLeaf(root->left))
        sum += root->left->data;
    else
        sum += leftLeavesSum(root->left);

    // Recursively process the right subtree.
    sum += leftLeavesSum(root->right);

    return sum;
}

int main()
{
    /*
             1
            / \
           2   3
          /   / \
         4   5   6
    */

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

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

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

    cout << leftLeavesSum(root) << endl;

    return 0;
}
Java
import java.util.*;

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

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

class GFG {

    // Check whether the given node is a leaf.
    static boolean isLeaf(Node node)
    {
        return node != null && node.left == null
            && node.right == null;
    }

    static int leftLeavesSum(Node root)
    {
        // Base case: empty tree.
        if (root == null)
            return 0;

        int sum = 0;

        // If the left child is a leaf, add its value.
        if (isLeaf(root.left))
            sum += root.left.data;
        else
            sum += leftLeavesSum(root.left);

        // Recursively process the right subtree.
        sum += leftLeavesSum(root.right);

        return sum;
    }
    public static void main(String[] args)
    {

        /*
                 1
                / \
               2   3
              /   / \
             4   5   6
        */

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

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

        root.right.left = new Node(5);
        root.right.right = new Node(6);
        
        System.out.println(leftLeavesSum(root));
    }
}
Python
# Definition for a binary tree node.
class Node:
    def __init__(self, val):
        self.data = val
        self.left = None
        self.right = None

# Check whether the given node is a leaf.
def isLeaf(node):
    return (node is not None and
            node.left is None and
            node.right is None)


def leftLeavesSum(root):
    # Base case: empty tree.
    if root is None:
        return 0

    sum = 0

    # If the left child is a leaf, add its value.
    if isLeaf(root.left):
        sum += root.left.data
    else:
        sum += leftLeavesSum(root.left)

    # Recursively process the right subtree.
    sum += leftLeavesSum(root.right)

    return sum


# Driver Code
if __name__ == "__main__":

    #       1
    #      / \
    #     2   3
    #    /   / \
    #   4   5   6

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

    root.left.left = Node(4)

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

    print(leftLeavesSum(root))
C#
using System;

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

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

class GFG {
    
    // Check whether the given node is a leaf.
    static bool IsLeaf(Node node)
    {
        return node != null && node.left == null
            && node.right == null;
    }

    static int leftLeavesSum(Node root)
    {
        // Base case: empty tree.
        if (root == null)
            return 0;

        int sum = 0;

        // If the left child is a leaf, add its value.
        if (IsLeaf(root.left))
            sum += root.left.data;
        else
            sum += leftLeavesSum(root.left);

        // Recursively process the right subtree.
        sum += leftLeavesSum(root.right);

        return sum;
    }
    
    static void Main()
    {
        /*
                 1
                / \
               2   3
              /   / \
             4   5   6
        */

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

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

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

        Console.WriteLine(leftLeavesSum(root));
    }
}
JavaScript
// Definition for a binary tree node.
class Node {
    constructor(val)
    {
        this.data = val;
        this.left = null;
        this.right = null;
    }
}

// Check whether the given node is a leaf.
function isLeaf(node)
{
    return node !== null && node.left === null
           && node.right === null;
}

function leftLeavesSum(root)
{
    // Base case: empty tree.
    if (root === null)
        return 0;

    let sum = 0;

    // If the left child is a leaf, add its value.
    if (isLeaf(root.left))
        sum += root.left.data;
    else
        sum += leftLeavesSum(root.left);

    // Recursively process the right subtree.
    sum += leftLeavesSum(root.right);

    return sum;
}

// Driver code

/*
        1
       / \
      2   3
     /   / \
    4   5   6
*/

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

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

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

console.log(leftLeavesSum(root));

Output
9

Using BFS(Level Order Traversal) - O(n) Time and O(n) Space

The idea is to perform a level-order traversal using a queue. For every node, first check its left child; if that child is a leaf, add its value to the sum.

  • If the tree is empty, return 0.
  • Insert the root into a queue and perform BFS.
  • For each node, check whether its left child is a leaf.
  • If it is, add its value to the sum.
  • Push the existing left and right children into the queue.
  • Return the accumulated sum after processing all nodes.
C++
#include <bits/stdc++.h>
using namespace std;

// Definition for a binary tree node.
class Node
{
  public:
    int data;
    Node *left;
    Node *right;

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

int leftLeavesSum(Node *root)
{
    // Base case: empty tree.
    if (root == nullptr)
        return 0;

    int sum = 0;
    queue<Node *> q;

    q.push(root);

    while (!q.empty())
    {
        Node *curr = q.front();
        q.pop();

        // Check whether the left child is a leaf.
        if (curr->left)
        {
            if (curr->left->left == nullptr && curr->left->right == nullptr)
            {

                // Add the left leaf's value.
                sum += curr->left->data;
            }

            // Add left child for further traversal.
            q.push(curr->left);
        }

        // Add right child for further traversal.
        if (curr->right)
            q.push(curr->right);
    }

    return sum;
}

int main()
{
    /*
             1
            / \
           2   3
          /   / \
         4   5   6
    */

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

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

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

    cout << leftLeavesSum(root) << endl;

    return 0;
}
Java
import java.util.*;

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

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

class GFG {
    static int leftLeavesSum(Node root)
    {
        // Base case: empty tree.
        if (root == null)
            return 0;

        int sum = 0;
        Queue<Node> q = new LinkedList<>();

        q.add(root);

        while (!q.isEmpty()) {
            Node curr = q.poll();

            // Check whether the left child is a leaf.
            if (curr.left != null) {
                if (curr.left.left == null
                    && curr.left.right == null) {

                    // Add the left leaf's value.
                    sum += curr.left.data;
                }

                // Add left child for further traversal.
                q.add(curr.left);
            }

            // Add right child for further traversal.
            if (curr.right != null)
                q.add(curr.right);
        }

        return sum;
    }
    
    public static void main(String[] args)
    {

        /*
                 1
                / \
               2   3
              /   / \
             4   5   6
        */

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

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

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

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


# Definition for a binary tree node.
class Node:
    def __init__(self, val):
        self.data = val
        self.left = None
        self.right = None


def leftLeavesSum(root):
    
    # Base case: empty tree.
    if root is None:
        return 0

    sum = 0
    q = deque()

    q.append(root)

    while q:
        curr = q.popleft()

        # Check whether the left child is a leaf.
        if curr.left:
            if curr.left.left is None and curr.left.right is None:

                # Add the left leaf's value.
                sum += curr.left.data

            # Add left child for further traversal.
            q.append(curr.left)

        # Add right child for further traversal.
        if curr.right:
            q.append(curr.right)

    return sum


# Driver Code
if __name__ == "__main__":

    #       1
    #      / \
    #     2   3
    #    /   / \
    #   4   5   6

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

    root.left.left = Node(4)

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

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

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

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

class GFG {
    static int leftLeavesSum(Node root)
    {
        // Base case: empty tree.
        if (root == null)
            return 0;

        int sum = 0;
        Queue<Node> q = new Queue<Node>();

        q.Enqueue(root);

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

            // Check whether the left child is a leaf.
            if (curr.left != null) {
                if (curr.left.left == null
                    && curr.left.right == null) {
                    // Add the left leaf's value.
                    sum += curr.left.data;
                }

                // Add left child for further traversal.
                q.Enqueue(curr.left);
            }

            // Add right child for further traversal.
            if (curr.right != null)
                q.Enqueue(curr.right);
        }

        return sum;
    }

    static void Main()
    {
        /*
                 1
                / \
               2   3
              /   / \
             4   5   6
        */

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

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

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

        Console.WriteLine(leftLeavesSum(root));
    }
}
JavaScript
// Definition for a binary tree node.
class Node {
    constructor(val)
    {
        this.data = val;
        this.left = null;
        this.right = null;
    }
}

function leftLeavesSum(root)
{
    // Base case: empty tree.
    if (root === null)
        return 0;

    let sum = 0;
    const q = [ root ];
    let front = 0;

    while (front < q.length) {
        const curr = q[front++];

        // Check whether the left child is a leaf.
        if (curr.left !== null) {
            if (curr.left.left === null
                && curr.left.right === null) {

                // Add the left leaf's value.
                sum += curr.left.data;
            }

            // Add left child for further traversal.
            q.push(curr.left);
        }

        // Add right child for further traversal.
        if (curr.right !== null)
            q.push(curr.right);
    }

    return sum;
}

// Driver code

/*
        1
       / \
      2   3
     /   / \
    4   5   6
*/

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

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

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

console.log(leftLeavesSum(root));

Output
9
Comment