# 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
# """
# if root is None:
# return 0
# if root.left is not None:
# if root.left.left is None and root.left.right is None:
# return root.left.val + self.sumOfLeftLeaves(root.right)
# return self.sumOfLeftLeaves(root.left) + self.sumOfLeftLeaves(root.right)
def sumOfLeftLeaves(self, root):
stack = [root]
res = 0
while len(stack) > 0:
curr = stack.pop(0)
if curr is not None:
if curr.left is not None:
if curr.left.left is None and curr.left.right is None:
res += curr.left.val
stack.insert(0, curr.right)
stack.insert(0, curr.left)
return res
Download Sum of Left Leaves.pyLeetcode 404 Sum of Left Leaves problem solution in python3 with explanation. This is the best place to expand your knowledge and get prepared for your next interview.
self.val = x
Feedback is the most important part of any website.
If you have any query, suggestion or feedback, Please feel free to contact us.