Path Sum*
, 5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1class Solution(object):
def hasPathSum(self, root, sum):
"""
:type root: TreeNode
:type sum: int
:rtype: bool
"""
if root == None:
return False
sum -= root.val
if root.left == None and root.right == None: #make sure it's reach the end of the path == 0
return sum == 0
if root.left and self.hasPathSum(root.left, sum):
return True
if root.right and self.hasPathSum(root.right, sum):
return True
return FalseLast updated