[easy][Tree]404.Sum of Left Leaves

原题是:

[easy][Tree]404.Sum of Left Leaves_第1张图片
Screen Shot 2017-11-07 at 3.19.33 PM.png

思路是:

区分出唯一的特殊情况:当左子节点存在又没有自己的任何子节点时,它就可以返回左侧的值了。

代码是:

# 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):
        
        if root == None :
            return 0
        elif root.left and not(root.left.left or root.left.right):
            L = root.left.val
        else :
            L = self.sumOfLeftLeaves(root.left)

        R = self.sumOfLeftLeaves(root.right)

        return L + R

你可能感兴趣的:([easy][Tree]404.Sum of Left Leaves)