python写算法题:leetcode: 112. Path Sum


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

 

你可能感兴趣的:(算法,python)