Find sum of all left leaves in a given Binary Tree | A binary tree node ; Pass in a sum variable as an accumulator ; Check whether this node is a leaf node and is left ; Pass 1 for left and 0 for right ; A wrapper over above recursive function ; Use the above recursive function to evaluate sum ; Let us construct the Binary Tree shown in the above figure
class Node : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . key = key NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def leftLeavesSumRec ( root , isLeft , summ ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return NEW_LINE DEDENT if root . left is None and root . right is None and isLeft == True : NEW_LINE INDENT summ [ 0 ] += root . key NEW_LINE DEDENT leftLeavesSumRec ( root . left , 1 , summ ) NEW_LINE leftLeavesSumRec ( root . right , 0 , summ ) NEW_LINE DEDENT def leftLeavesSum ( root ) : NEW_LINE INDENT summ = [ 0 ] NEW_LINE leftLeavesSumRec ( root , 0 , summ ) NEW_LINE return summ [ 0 ] NEW_LINE DEDENT root = Node ( 20 ) ; NEW_LINE root . left = Node ( 9 ) ; NEW_LINE root . right = Node ( 49 ) ; NEW_LINE root . right . left = Node ( 23 ) ; NEW_LINE root . right . right = Node ( 52 ) ; NEW_LINE root . right . right . left = Node ( 50 ) ; NEW_LINE root . left . left = Node ( 5 ) ; NEW_LINE root . left . right = Node ( 12 ) ; NEW_LINE root . left . right . right = Node ( 12 ) ; NEW_LINE print " Sum ▁ of ▁ left ▁ leaves ▁ is " , leftLeavesSum ( root ) NEW_LINE