easy understanding python solution


  • 2
    G

    Well, the idea is simple, and I write three functions to make this pretty clear, just for your reference :-)

    # 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 sumOfLeftLeaves(self, root):
            """
            :type root: TreeNode
            :rtype: int
            """
            self.ans=0
            self.findleftleaves(root)
            return self.ans
            
        def findleftleaves(self,root):
            if not root:
                return
            if root.left and self.isLeaves(root.left):
                self.ans+=root.left.val
            self.findleftleaves(root.left)
            self.findleftleaves(root.right)
            return 
                
        def isLeaves(self,root):
            return root.left==None and root.right==None
    
    

Log in to reply
 

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