-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path112.py
More file actions
22 lines (20 loc) · 679 Bytes
/
112.py
File metadata and controls
22 lines (20 loc) · 679 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution:
def hasPathSum(self, root, targetSum):
if root is None:
return False
def sum_path(root, s, targetSum):
if root.left is None and root.right is None:
if s+root.val ==targetSum:
return True
else:
return False
s=root.val+s
f1, f2= False, False
if root.left:
f1=sum_path(root.left, s, targetSum)
if root.right:
f2=sum_path(root.right, s, targetSum)
if f1 or f2:
return True
return False
return sum_path(root, 0, targetSum)