Skip to content

Files

Latest commit

940d2f9 · Jul 28, 2016

History

History
30 lines (22 loc) · 583 Bytes

112._path_sum.md

File metadata and controls

30 lines (22 loc) · 583 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 root == None:
                return False
        else:
            if root.val == sum and (root.left == None and root.right == None):
                return True
            else:
                return self.hasPathSum(root.left, sum - root.val) or self.hasPathSum(root.right, sum - root.val)