Skip to main content

404. Sum of Left Leaf

https://leetcode.com/problems/sum-of-left-leaves

Python

from typing import Optional


class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right


class Solution:
def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
if not root:
return 0

sum = self.sumOfLeftLeaves(root.left) + self.sumOfLeftLeaves(root.right)
if root.left and root.left.left is None and root.left.right is None:
sum += root.left.val

return sum