Find sum of all left leaves in a given Binary Tree | Java program to find sum of all left leaves ; A binary tree node ; constructor to create a new Node ; Return the sum of left leaf nodes ; A queue of pairs to do bfs traversal and keep track if the node is a left or right child if boolean value is true then it is a left child . ; do bfs traversal ; if temp is a leaf node and left child of its parent ; if it is not leaf then push its children nodes into queue ; boolean value is true here because it is left child of its parent ; boolean value is false here because it is right child of its parent ; Driver Code
import java . util . * ; class GFG { static class Node { int key ; Node left , right ; Node ( int key_ ) { key = key_ ; left = null ; right = null ; } } ; static class pair { Node first ; boolean second ; public pair ( Node first , boolean second ) { this . first = first ; this . second = second ; } } static int sumOfLeftLeaves ( Node root ) { if ( root == null ) return 0 ; Queue < pair > q = new LinkedList < > ( ) ; q . add ( new pair ( root , false ) ) ; int sum = 0 ; while ( ! q . isEmpty ( ) ) { Node temp = q . peek ( ) . first ; boolean is_left_child = q . peek ( ) . second ; q . remove ( ) ; if ( is_left_child ) sum = sum + temp . key ; if ( temp . left != null && temp . right != null && is_left_child ) sum = sum - temp . key ; if ( temp . left != null ) { q . add ( new pair ( temp . left , true ) ) ; } if ( temp . right != null ) { q . add ( new pair ( temp . right , false ) ) ; } } return sum ; } public static void main ( String [ ] args ) { 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 ) ; System . out . print ( " Sum ▁ of ▁ left ▁ leaves ▁ is ▁ " + sumOfLeftLeaves ( root ) + "NEW_LINE"); } }