112. Path Sum
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
def path_sum(node,s):
if not node:
return False
s += node.val
# 代表走到終點
if not node.left and not node.right and s == targetSum:
return True
return path_sum(node.left,s) or path_sum(node.right,s)
return path_sum(root,0)
題目重點:
其他
Last updated