437. Path Sum III (Medium) - TengnanYao/daily_leetcode GitHub Wiki
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
def pathSum(self, root, targetSum):
"""
:type root: TreeNode
:type targetSum: int
:rtype: int
"""
self.result = 0
def calSum(root, s, cur):
if root:
cur += root.val
if cur == s:
self.result += 1
calSum(root.left, s, cur)
calSum(root.right, s, cur)
def dfs(root, s, cur):
if root:
calSum(root, s, cur)
dfs(root.left, s, cur)
dfs(root.right, s, cur)
dfs(root, targetSum, 0)
return self.result