# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
if root is None:
return 0
if root.left is None and root.right is None:
return 0
leftvalue = self.sumOfLeftLeaves(root.left)
if root.left and not root.left.left and not root.left.right:
leftvalue = root.left.val
rightvalue = self.sumOfLeftLeaves(root.right)
return leftvalue + rightvalue
404. 左叶子之和
最新推荐文章于 2024-11-04 21:51:40 发布
本文介绍了如何使用Python定义一个名为Solution的类,其中包含一个方法sumOfLeftLeaves,用于计算给定二叉树中所有左叶子节点值的和。方法通过递归遍历左子树并处理特殊情况来实现。
摘要由CSDN通过智能技术生成