##Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
class Solution:
# @param sum, an integer
# @return a boolean
def (self, root, sum):
if root is None:
return False
if root.right is None and root.left is None:
return root.val == sum
return self.hasPathSum(root.left, sum - root.val) or self.hasPathSum(root.right, sum - root.val)
近期评论