Given a Binary Tree, return the sumof 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]
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]
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.
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>usingnamespacestd;// Definition for a binary tree node.classNode{public:intdata;Node*left;Node*right;Node(intval){data=val;left=right=nullptr;}};// Check whether the given node is a leaf.boolisLeaf(Node*node){returnnode!=nullptr&&node->left==nullptr&&node->right==nullptr;}intleftLeavesSum(Node*root){// Base case: empty tree.if(root==nullptr)return0;intsum=0;// If the left child is a leaf, add its value.if(isLeaf(root->left))sum+=root->left->data;elsesum+=leftLeavesSum(root->left);// Recursively process the right subtree.sum+=leftLeavesSum(root->right);returnsum;}intmain(){/* 1 / \ 2 3 / / \ 4 5 6 */Node*root=newNode(1);root->left=newNode(2);root->right=newNode(3);root->left->left=newNode(4);root->right->left=newNode(5);root->right->right=newNode(6);cout<<leftLeavesSum(root)<<endl;return0;}
Java
importjava.util.*;classNode{intdata;Nodeleft;Noderight;Node(intval){data=val;left=right=null;}}classGFG{// Check whether the given node is a leaf.staticbooleanisLeaf(Nodenode){returnnode!=null&&node.left==null&&node.right==null;}staticintleftLeavesSum(Noderoot){// Base case: empty tree.if(root==null)return0;intsum=0;// If the left child is a leaf, add its value.if(isLeaf(root.left))sum+=root.left.data;elsesum+=leftLeavesSum(root.left);// Recursively process the right subtree.sum+=leftLeavesSum(root.right);returnsum;}publicstaticvoidmain(String[]args){/* 1 / \ 2 3 / / \ 4 5 6 */Noderoot=newNode(1);root.left=newNode(2);root.right=newNode(3);root.left.left=newNode(4);root.right.left=newNode(5);root.right.right=newNode(6);System.out.println(leftLeavesSum(root));}}
Python
# Definition for a binary tree node.classNode:def__init__(self,val):self.data=valself.left=Noneself.right=None# Check whether the given node is a leaf.defisLeaf(node):return(nodeisnotNoneandnode.leftisNoneandnode.rightisNone)defleftLeavesSum(root):# Base case: empty tree.ifrootisNone:return0sum=0# If the left child is a leaf, add its value.ifisLeaf(root.left):sum+=root.left.dataelse:sum+=leftLeavesSum(root.left)# Recursively process the right subtree.sum+=leftLeavesSum(root.right)returnsum# Driver Codeif__name__=="__main__":# 1# / \# 2 3# / / \# 4 5 6root=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#
usingSystem;classNode{publicintdata;publicNodeleft;publicNoderight;publicNode(intval){data=val;left=null;right=null;}}classGFG{// Check whether the given node is a leaf.staticboolIsLeaf(Nodenode){returnnode!=null&&node.left==null&&node.right==null;}staticintleftLeavesSum(Noderoot){// Base case: empty tree.if(root==null)return0;intsum=0;// If the left child is a leaf, add its value.if(IsLeaf(root.left))sum+=root.left.data;elsesum+=leftLeavesSum(root.left);// Recursively process the right subtree.sum+=leftLeavesSum(root.right);returnsum;}staticvoidMain(){/* 1 / \ 2 3 / / \ 4 5 6 */Noderoot=newNode(1);root.left=newNode(2);root.right=newNode(3);root.left.left=newNode(4);root.right.left=newNode(5);root.right.right=newNode(6);Console.WriteLine(leftLeavesSum(root));}}
JavaScript
// Definition for a binary tree node.classNode{constructor(val){this.data=val;this.left=null;this.right=null;}}// Check whether the given node is a leaf.functionisLeaf(node){returnnode!==null&&node.left===null&&node.right===null;}functionleftLeavesSum(root){// Base case: empty tree.if(root===null)return0;letsum=0;// If the left child is a leaf, add its value.if(isLeaf(root.left))sum+=root.left.data;elsesum+=leftLeavesSum(root.left);// Recursively process the right subtree.sum+=leftLeavesSum(root.right);returnsum;}// Driver code/* 1 / \ 2 3 / / \ 4 5 6*/constroot=newNode(1);root.left=newNode(2);root.right=newNode(3);root.left.left=newNode(4);root.right.left=newNode(5);root.right.right=newNode(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>usingnamespacestd;// Definition for a binary tree node.classNode{public:intdata;Node*left;Node*right;Node(intval){data=val;left=right=nullptr;}};intleftLeavesSum(Node*root){// Base case: empty tree.if(root==nullptr)return0;intsum=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);}returnsum;}intmain(){/* 1 / \ 2 3 / / \ 4 5 6 */Node*root=newNode(1);root->left=newNode(2);root->right=newNode(3);root->left->left=newNode(4);root->right->left=newNode(5);root->right->right=newNode(6);cout<<leftLeavesSum(root)<<endl;return0;}
Java
importjava.util.*;classNode{intdata;Nodeleft;Noderight;Node(intval){data=val;left=right=null;}}classGFG{staticintleftLeavesSum(Noderoot){// Base case: empty tree.if(root==null)return0;intsum=0;Queue<Node>q=newLinkedList<>();q.add(root);while(!q.isEmpty()){Nodecurr=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);}returnsum;}publicstaticvoidmain(String[]args){/* 1 / \ 2 3 / / \ 4 5 6 */Noderoot=newNode(1);root.left=newNode(2);root.right=newNode(3);root.left.left=newNode(4);root.right.left=newNode(5);root.right.right=newNode(6);System.out.println(leftLeavesSum(root));}}
Python
fromcollectionsimportdeque# Definition for a binary tree node.classNode:def__init__(self,val):self.data=valself.left=Noneself.right=NonedefleftLeavesSum(root):# Base case: empty tree.ifrootisNone:return0sum=0q=deque()q.append(root)whileq:curr=q.popleft()# Check whether the left child is a leaf.ifcurr.left:ifcurr.left.leftisNoneandcurr.left.rightisNone:# 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.ifcurr.right:q.append(curr.right)returnsum# Driver Codeif__name__=="__main__":# 1# / \# 2 3# / / \# 4 5 6root=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#
usingSystem;usingSystem.Collections.Generic;classNode{publicintdata;publicNodeleft;publicNoderight;publicNode(intval){data=val;left=null;right=null;}}classGFG{staticintleftLeavesSum(Noderoot){// Base case: empty tree.if(root==null)return0;intsum=0;Queue<Node>q=newQueue<Node>();q.Enqueue(root);while(q.Count>0){Nodecurr=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);}returnsum;}staticvoidMain(){/* 1 / \ 2 3 / / \ 4 5 6 */Noderoot=newNode(1);root.left=newNode(2);root.right=newNode(3);root.left.left=newNode(4);root.right.left=newNode(5);root.right.right=newNode(6);Console.WriteLine(leftLeavesSum(root));}}
JavaScript
// Definition for a binary tree node.classNode{constructor(val){this.data=val;this.left=null;this.right=null;}}functionleftLeavesSum(root){// Base case: empty tree.if(root===null)return0;letsum=0;constq=[root];letfront=0;while(front<q.length){constcurr=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);}returnsum;}// Driver code/* 1 / \ 2 3 / / \ 4 5 6*/constroot=newNode(1);root.left=newNode(2);root.right=newNode(3);root.left.left=newNode(4);root.right.left=newNode(5);root.right.right=newNode(6);console.log(leftLeavesSum(root));