Skip to content

Latest commit

 

History

History
28 lines (21 loc) · 513 Bytes

112._path_sum.md

File metadata and controls

28 lines (21 loc) · 513 Bytes

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