广度优先遍历,记录当前节点是否为左节点,再判断节点是否为叶子节点
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param root TreeNode类
# @return int整型
#
class Solution:
def sumOfLeftLeaves(self , root: TreeNode) -> int:
# write code here
if not root: return 0
queue = [[0, root]]
res = 0
while queue:
f, node = queue.pop(0)
if not node.left and not node.right and f:
res += node.val
if node.left:
queue.append([1, node.left])
if node.right:
queue.append([0, node.right])
return res