【LeetCode】112. Path Sum

版权声明:本文为博主原创文章,请尊重原创,转载请注明原文地址和作者信息! https://blog.csdn.net/zzc15806/article/details/81293604

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

class Solution:
    def hasPathSum(self, root, sums):
        """
        :type root: TreeNode
        :type sum: int
        :rtype: bool
        """
        # 递归
        if root == None:
            return False
        if root.left == None and root.right == None:
            return root.val == sums
        return self.hasPathSum(root.left, sums - root.val) or self.hasPathSum(root.right, sums - root.val)

猜你喜欢

转载自blog.csdn.net/zzc15806/article/details/81293604