-
Notifications
You must be signed in to change notification settings - Fork 0
/
path-sum.py
29 lines (22 loc) · 981 Bytes
/
path-sum.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
27
28
29
# Definition for a binary tree node.
from typing import Optional
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], targetSum: int) -> int:
if root is None:
return 0
return self.dfs(root, targetSum) + self.pathSum(root.left, targetSum) + self.pathSum(root.right, targetSum)
def dfs(self, root: Optional[TreeNode], targetSum: int) -> int:
if root is None:
return 0
l = self.dfs(root.left, targetSum - root.val)
r = self.dfs(root.right, targetSum - root.val)
return l + r + (1 if targetSum == root.val else 0)
if __name__ == "__main__":
s = Solution()
print(s.pathSum(TreeNode(10, TreeNode(5, TreeNode(3, TreeNode(3), TreeNode(-2)), TreeNode(2, None, TreeNode(1))),
TreeNode(-3, None, TreeNode(11))), 3))