Simple Python 6 line easy to Understand


  • 0
    N
    # Definition for a binary tree node.
    # class TreeNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution(object):
        def util(self, node, ctr):
            if(node== None):
                return 0
            if(node.left== None and node.right== None):
                if(ctr): return node.val
                else: return 0
            else:
                return self.util(node.left, 1) + self.util(node.right, 0)
            
        def sumOfLeftLeaves(self, root):
            """
            :type root: TreeNode
            :rtype: int
            """
            return self.util(root, 0)
    

Log in to reply
 

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