-
Notifications
You must be signed in to change notification settings - Fork 2
/
437PathSumIII.py
55 lines (46 loc) · 1.38 KB
/
437PathSumIII.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
# main function: pathSum
# extra function: helper()
# double recursive
class Solution:
def pathSum(self, root: TreeNode, sumn: int) -> int:
if not root:
return 0
def helper(root, sumn) -> int:
ins = 0
if not root:
return 0
if root.val == sumn:
ins = 1
return ins + helper(root.left, sumn-root.val) + helper(root.right, sumn-root.val)
return helper(root, sumn) + self.pathSum(root.left,sumn) + self.pathSum(root.right, sumn)
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
# Solution2
class Solution:
def __init__(self):
self.ans = 0
def pathSum(self, root: TreeNode, sumn: int) -> int:
if not root:
return 0
self.dfs(root, sumn)
self.pathSum(root.left, sumn)
self.pathSum(root.right, sumn)
return self.ans
def dfs(self, root, sumn):
if not root:
return
sumn -= root.val
if not sumn:
self.ans += 1
self.dfs(root.left, sumn)
self.dfs(root.right, sumn)