Skip to content

Latest commit

 

History

History
28 lines (23 loc) · 613 Bytes

112. Path Sum-python.md

File metadata and controls

28 lines (23 loc) · 613 Bytes
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

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

        left = self.hasPathSum(root.left,sum)
        right = self.hasPathSum(root.right,sum)

        return left or right