Find sum of all left leaves in a given Binary Tree | A binary tree Node has key , pointer to left and right children ; A utility function to check if a given node is leaf or not ; This function returns sum of all left leaves in a given binary tree ; Initialize result ; Update result if root is not NULL ; If left of root is NULL , then add key of left child ; Else recur for left child of root ; Recur for right child of root and update res ; return result ; Driver program
class Node { constructor ( k ) { this . data = k ; this . left = null ; this . right = null ; } } function isLeaf ( node ) { if ( node == null ) return false ; if ( node . left == null && node . right == null ) return true ; return false ; } function leftLeavesSum ( node ) { let res = 0 ; if ( node != null ) { if ( isLeaf ( node . left ) ) res += node . left . data ; else res += leftLeavesSum ( node . left ) ; res += leftLeavesSum ( node . right ) ; } return res ; } root = new Node ( 20 ) ; root . left = new Node ( 9 ) ; root . right = new Node ( 49 ) ; root . left . right = new Node ( 12 ) ; root . left . left = new Node ( 5 ) ; root . right . left = new Node ( 23 ) ; root . right . right = new Node ( 52 ) ; root . left . right . right = new Node ( 12 ) ; root . right . right . left = new Node ( 50 ) ; document . write ( " " + leftLeavesSum ( root ) ) ;