Simple Python recursive.


  • 0
    J

    Use a flag:

    class Solution(object):
        def sumOfLeftLeaves(self, root, isLeftChild = False):
            """
            :type root: TreeNode
            :type isLeftChild: bool
            :rtype: int
            """
            if not root:
                return 0
            if not root.left and not root.right:
                return root.val if isLeftChild else 0
            return self.sumOfLeftLeaves(root.left, True) + self.sumOfLeftLeaves(root.right, False)
    

Log in to reply
 

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