首页 > 其他 > 详细

递归与二叉树_leetcode437

时间:2019-03-17 14:21:56      阅读:158      评论:0      收藏:0      [点我收藏+]
# 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


递归与二叉树_leetcode437

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!