Find sum of all left leaves in a given Binary Tree | C ++ program to find sum of all left leaves ; A binary tree node ; A constructor to create a new Node ; Return the sum of left leaf nodes ; Using a stack_ for Depth - First Traversal of the tree ; sum holds the sum of all the left leaves ; Check if currentNode 's left  child is a leaf node ; if currentNode is a leaf , add its data to the sum ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; class Node { public : int key ; Node * left , * right ; Node ( int key_ ) { key = key_ ; left = NULL ; right = NULL ; } } ; int sumOfLeftLeaves ( Node * root ) { if ( root == NULL ) return 0 ; stack < Node * > stack_ ; stack_ . push ( root ) ; int sum = 0 ; while ( stack_ . size ( ) > 0 ) { Node * currentNode = stack_ . top ( ) ; stack_ . pop ( ) ; if ( currentNode -> left != NULL ) { stack_ . push ( currentNode -> left ) ; if ( currentNode -> left -> left == NULL && currentNode -> left -> right == NULL ) { sum = sum + currentNode -> left -> key ; } } if ( currentNode -> right != NULL ) stack_ . push ( currentNode -> right ) ; } return sum ; } int main ( ) { Node * root = new Node ( 20 ) ; root -> left = new Node ( 9 ) ; root -> right = new Node ( 49 ) ; root -> right -> left = new Node ( 23 ) ; root -> right -> right = new Node ( 52 ) ; root -> right -> right -> left = new Node ( 50 ) ; root -> left -> left = new Node ( 5 ) ; root -> left -> right = new Node ( 12 ) ; root -> left -> right -> right = new Node ( 12 ) ; cout << " Sum ▁ of ▁ left ▁ leaves ▁ is ▁ " << sumOfLeftLeaves ( root ) << endl ; return 0 ; }