-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0112.py
26 lines (21 loc) · 785 Bytes
/
0112.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
25
26
class Solution:
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
if not root:
return []
# global target_count
target_count = 0
s = []
def traverse(node, s):
if node.right:
s.append(node.val)
traverse(node.right, s)
s.pop(-1)
if node.left:
s.append(node.val)
traverse(node.left, s)
s.pop(-1)
if not node.right and not node.left and sum(s) + node.val == targetSum:
nonlocal target_count
target_count += 1
traverse(root, s)
return bool(target_count)