Skip to content

112 path sum

112. Path Sum

题目: https://leetcode.com/problems/path-sum/

难度:

Easy

递归

class Solution(object):
    def hasPathSum(self, root, sum):
        """
        :type root: TreeNode
        :type sum: int
        :rtype: bool
        """
        if not root:
            return False
        if root.left or root.right:
            return self.hasPathSum(root.left, sum-root.val) or self.hasPathSum(root.right, sum-root.val)
        else:
            return root.val == sum          


回到顶部