python - path sum I

  • 0
    # Definition for a binary tree node.
    # class TreeNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    class Solution(object):
        def hasPathSum(self, root, sum):
            :type root: TreeNode
            :type sum: int
            :rtype: bool
            if not root:
                return False
            if not (root.left or root.right):
                return (sum - root.val) == 0
            return self.hasPathSum(root.left, sum - root.val) or self.hasPathSum(root.right, sum - root.val)

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.