Re: [闲聊] 每日leetcode

楼主: sustainer123 (caster)   2024-04-14 16:15:36
https://leetcode.com/problems/sum-of-left-leaves
404. Sum of Left Leaves
求左子叶和
Python Code:
# 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:
result = 0
def dfs(node):
nonlocal result
if not node:
return
if node.left and not node.left.left and not node.left.right:
result += node.left.val
dfs(node.left)
dfs(node.right)
dfs(root)
return result
写得好丑 等等看一下解答
然后昨天hard还没写 哇哇呜呜呜
作者: wwndbk (黑人问号)   2023-04-14 16:15:00
大师
楼主: sustainer123 (caster)   2024-04-14 16:16:00
我是ez守门员
作者: SecondRun (雨夜琴声)   2024-04-14 16:18:00
大师

Links booklink

Contact Us: admin [ a t ] ucptt.com