Path Sum

# Definition for a  binary tree node
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None
class Solution:
    # @param root, a tree node
    # @param sum, an integer
    # @return a boolean
    def hasPathSum(self, root, sum):
        if root==None :
                return False            
        if root.val==sum and root.left is None and root.right is None:
            return True
        #elif  root.val==sum:  #出现负数的情况,这里就不对了,可能这个条件成立了,但是子树之和为0 也是正确的
        #    return False
        else:
      
return (self.hasPathSum(root.left, sum-root.val) or self.hasPathSum(root.right, sum-root.val))

 

posted @ 2014-08-29 15:51  clq.lib  阅读(155)  评论(0编辑  收藏  举报