原题是:
思路是:
区分出唯一的特殊情况:当左子节点存在又没有自己的任何子节点时,它就可以返回左侧的值了。
代码是:
# 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