Skip to content

Latest commit

 

History

History
26 lines (24 loc) · 761 Bytes

112.md

File metadata and controls

26 lines (24 loc) · 761 Bytes

112. Path Sum

Solution 1

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def hasPathSum(self, root, targetSum):
        """
        :type root: TreeNode
        :type targetSum: int
        :rtype: bool
        """
        if root is None:
            return False
        if root.left is None and root.right is None:
            return targetSum == root.val
        left_ans = self.hasPathSum(root.left, targetSum - root.val)
        right_ans = self.hasPathSum(root.right, targetSum - root.val)
        return left_ans or right_ans