2023.6.14
返还条件是遇到空节点或叶子节点时返还
递归逻辑是节点的值等于左孩子加右孩子的值,
其中当左孩子为左叶子节点时就会返还其节点值
# 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 == None: return 0if (root.left is None) and (root.right is None): return 0 # 迭代返还条件if (root.left is not None) and (root.left.left is None) and (root.left.right is None): left = root.left.valelse: left = self.sumOfLeftLeaves(root.left)right = self.sumOfLeftLeaves(root.right)return left + right