解题思路:
这就是一个很简单的二叉树搜索算法,本次采用的深度优先算法,具体代码如下:
# 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:
res = 0
if not root:
return 0
if root.left and not root.left.left and not root.left.right:
res+=root.left.val
return res+self.sumOfLeftLeaves(root.left)+self.sumOfLeftLeaves(root.right)