class Solution(object):
    def sumOfLeftLeaves(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if not root:
            return 0#空树
        if root.left!=None and root.left.left==None and root.left.right==None:#边界,左节点不为空且左节点的左节点以及左节点的右节点为空
            return root.left.val+self.sumOfLeftLeaves(root.right)#左节点的值加上右节点的递归
        return self.sumOfLeftLeaves(root.left)+self.sumOfLeftLeaves(root.right)#递归

 


本文转载:CSDN博客