题解 | #左叶子之和#
左叶子之和
https://www.nowcoder.com/practice/405a9033800b403ba8b7b905bab0463d
# 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
left_node_sum = 0
if root.left and root.left.left == None and root.left.right == None:
left_node_sum += root.left.val
# 我服了这个self.xxx
left_node_sum += self.sumOfLeftLeaves(root.left) + self.sumOfLeftLeaves(root.right)
return left_node_sum

查看17道真题和解析