forked from Kyrylo-Ktl/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPath Sum II.py
More file actions
30 lines (23 loc) · 889 Bytes
/
Path Sum II.py
File metadata and controls
30 lines (23 loc) · 889 Bytes
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
27
28
29
30
from typing import List, Optional, Generator
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def pathSum(self, root: Optional[TreeNode], target_sum: int) -> Generator:
return self.dfs(root, [], target_sum)
@classmethod
def dfs(cls, tree: Optional[TreeNode], cur_path: List[int], target: int) -> Generator:
if tree is None:
return None
cur_path.append(tree.val)
target -= tree.val
if cls.is_leaf(tree) and not target:
yield cur_path
else:
yield from cls.dfs( tree.left, cur_path[:], target)
yield from cls.dfs(tree.right, cur_path[:], target)
@classmethod
def is_leaf(cls, node: TreeNode) -> bool:
return node.left is None and node.right is None