题解 | #二叉树中和为某一值的路径(三)#

二叉树中和为某一值的路径(三)

https://www.nowcoder.com/practice/965fef32cae14a17a8e86c76ffe3131f

# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param root TreeNode类 
# @param sum int整型 
# @return int整型
#
class Solution:
    def __init__(self) -> None:
        self.count = 0

    # 任意一个遍历都行
    # 因为不需要从根节点开始,也不需要在叶子节点结束 
    # 所以必须要全部走一遍
    def preorder(self, root,sum):
        if not root: return
        self.countsum(root,sum)
        self.preorder(root.left, sum)
        self.preorder(root.right, sum)
        return

    # 计算每一个值 
    def countsum(self,root,sum):
        if not root: return
        if sum - root.val == 0: 
            self.count += 1
            # 因为可能会有0的出现 所以不能return
            # return self.count
        self.countsum(root.left, sum - root.val)
        self.countsum(root.right, sum - root.val)
        return self.count

    # 不需要从根节点开始,也不需要在叶子节点结束
    def FindPath(self , root: TreeNode, sum: int) -> int:
        # write code here
        if not root:
            return 0
        self.preorder(root, sum)
        return self.count

全部评论

相关推荐

FightingNa...:小厂不喜欢离毕业还远的。培养你三个月小半年,你又回去上学,你丰富简历爽歪歪,小厂啥也得不到。大厂兴许愿意培养你,可以试试大厂,准备下不黑了就行。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务