---
# System prepended metadata

title: 0404. Sum of Left Leaves
tags: [Leetcode, Tree, Recursion, Easy]

---

# 0404. Sum of Left Leaves
###### tags: `Leetcode` `Easy` `Tree` `Recursion`
Link: https://leetcode.com/problems/sum-of-left-leaves/description/
## Code
```java=
class Solution {
    public int sumOfLeftLeaves(TreeNode root) {
        return dfs(root, false);
    }
    public int dfs(TreeNode root, boolean isLeft){
        if(root==null) return 0;
        if(root.left==null && root.right==null) return isLeft?root.val:0;
        return dfs(root.left, true)+dfs(root.right, false);
    }
}
```
```python=
class Solution:
    def sumOfLeftLeaves(self, root: Optional[TreeNode], isLeft=False) -> int:
        if not root:  return 0
        if not root.left and not root.right: return root.val if isLeft else 0
        return self.sumOfLeftLeaves(root.left, True)+self.sumOfLeftLeaves(root.right, False)
```