计算给定二叉树的所有左叶子之和。
示例:
3
/ \
9 20
/ \
15 7
在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24
class Solution:
def sumOfLeftLeaves(self, root: TreeNode) -> int:
result = 0;
if root == None:
return 0
if root.left:
if not root.left.left and not root.left.right:
result += root.left.val
else:
result +=self.sumOfLeftLeaves(root.left)
if root.right:
result += self.sumOfLeftLeaves(root.right)
return result