Easy to understand Python Solution:


  • 0
    S

    Algorithm:

    1. Have a boolean flag set to True if it's coming from the left node and False otherwise
    2. The variable "total" would have the sum of all the left leaf nodes
    def sumOfLeftLeaves(self, root):
            """
            :type root: TreeNode
            :rtype: int
            """
            def helper(root, left_flag):
                if not root.left and not root.right and left_flag:
                    self.total += root.val
                if root.left:
                    helper(root.left, True)
                if root.right:
                    helper(root.right, False)
                return self.total
                    
            if not root:
                return 0
            self.total = 0
            return helper(root, False)
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.