递归与二叉树_leetcode437

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

class Solution(object):
def pathSum(self, root, sum):
"""
:type root: TreeNode
:type sum: int
:rtype: int
"""
if not root :
return 0

return self.findPath(root,sum) + self.pathSum(root.left,sum) + self.pathSum(root.right,sum)

def findPath(self,root,sum):

if not root :
return 0

res = 0

if root.val == sum :
res += 1

res += self.findPath(root.left,sum-root.val)
res += self.findPath(root.right,sum-root.val)

return res


原文地址:https://www.cnblogs.com/lux-ace/p/10546817.html