-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path437.路径总和-iii.py
36 lines (30 loc) · 964 Bytes
/
437.路径总和-iii.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
#
# @lc app=LeetCode.cn id=437 lang=python3
#
# [437] 路径总和 III
#
# @lc code=start
# Definition for a binary tree node.
# 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: TreeNode, targetSum: int) -> int:
if root == None:
return 0
res = self.pathSumStartsWith(root, targetSum) + \
self.pathSum(root.left, targetSum)+ \
self.pathSum(root.right, targetSum)
return res
def pathSumStartsWith(self, root, targetSum):
if root == None:
return 0
count = 0
if root.val == targetSum:
count = 1
count += self.pathSumStartsWith(root.left, targetSum-root.val)
count += self.pathSumStartsWith(root.right, targetSum-root.val)
return count
# @lc code=end