Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

Leetcode Sum of Left Leaves problem solution

YASH PAL, 31 July 202422 January 2026

In this Leetcode Sum of Left Leaves problem solution we have given the root of a binary tree, return the sum of all left leaves.

Leetcode Sum of Left Leaves problem solution

Leetcode Sum of Left Leaves problem solution in Python.

class Solution(object):
    def sumOfLeftLeaves(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        self.ans=0
        self.findleftleaves(root)
        return self.ans
        
    def findleftleaves(self,root):
        if not root:
            return
        if root.left and self.isLeaves(root.left):
            self.ans+=root.left.val
        self.findleftleaves(root.left)
        self.findleftleaves(root.right)
        return 
            
    def isLeaves(self,root):
        return root.left==None and root.right==None

Sum of Left Leaves problem solution in Java.

public int sumOfLeftLeaves(TreeNode root) {
	if (root == null) return 0;

	int sum = 0;
	if (root.left != null && root.left.left == null && root.left.right == null) {
		sum = root.left.val;
	}

	return sum + sumOfLeftLeaves(root.left) + sumOfLeftLeaves(root.right);
}

Problem solution in C++.

class Solution {
public:
    int sumOfLeftLeaves(TreeNode* root) {
       if(root == nullptr) 
           return 0;
    
       int sum = 0;
       queue<pair<TreeNode*, bool>> q;
        
       q.push({root, false});
       while(!q.empty())
       {
           pair<TreeNode*, bool> p = q.front();
           q.pop();
           TreeNode *node = p.first;
           bool bIsLeftNode = p.second;
           
           if(node->left)
           {
               q.push({node->left, true});
           }
           if(node->right)
           {
               q.push({node->right, false});
           }
           
           if(bIsLeftNode && node->left == nullptr && node->right == nullptr)
           {
               sum+= node->val;
           }
       }
        return sum;
    }
};

Problem solution in C.

void countleft(struct TreeNode* root,int *sum)
{
       if(root==NULL) {
           return;
       }
    if(root->left == NULL && root->right == NULL)
       *sum= *sum+root->val;    

}

void traverse(struct TreeNode* root,int *sum)
{
    if(root==NULL) {
        return;
    }
    traverse(root->left,sum);
    countleft(root->left,sum);
    traverse(root->right,sum);
}

int sumOfLeftLeaves(struct TreeNode* root){
      int sum=0;
      if(root==NULL || (root->left == 0 && root->right==0)) {
          return 0;
      }
      traverse(root,&sum);
      return sum;
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes