LeetCode 437. 路径总和 III (Path Sum III)[简单]

lework · 2020年06月14日 · 最后由 lework 回复于 2020年06月14日 · 145 次阅读

给定一个二叉树,它的每个结点都存放着一个整数值。

找出路径和等于给定数值的路径总数。

路径不需要从根节点开始,也不需要在叶子节点结束,但是路径方向必须是向下的(只能从父节点到子节点)。

二叉树不超过 1000 个节点,且节点数值范围是 [-1000000,1000000] 的整数。

示例:

root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8

      10
     /  \
    5   -3
   / \    \
  3   2   11
 / \   \
3  -2   1

返回 3。和等于 8 的路径有:

1.  5 -> 3
2.  5 -> 2 -> 1
3.  -3 -> 11

来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/path-sum-iii 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

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

class Solution:
    def pathSum(self, root, sum):
        if root == None:
            return 0
        return self.count(root,sum)+self.pathSum(root.left,sum)+self.pathSum(root.right,sum)
    def count(self,root,sum):
        if root ==None:
            return 0
        return int(root.val == sum) + self.count(root.left,sum-root.val) + self.count(root.right,sum-root.val)
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册