# 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) ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up