-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe112.py
24 lines (21 loc) · 819 Bytes
/
e112.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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 hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
if not root :
return False
def dfs(curr: Optional[TreeNode], remaining: int) -> bool :
if not curr :
return remaining == 0
remaining -= curr.val
if curr.left and curr.right :
return dfs(curr.left, remaining) or \
dfs(curr.right, remaining)
if curr.left :
return dfs(curr.left, remaining)
return dfs(curr.right, remaining)
return dfs(root, targetSum)