计算给定二叉树的所有左叶子之和。
示例:
3
/ \
9 20
/ \
15 7 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24
# 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
"""
tmp = 0
if root:
left, right = root.left,root.right
if left and(left.left or left.right) is None:
tmp+=left.val
tmp += self.sumOfLeftLeaves(left)+self.sumOfLeftLeaves(right)
return tmp