首页 > 其他 > 详细

Path Sum

时间:2014-08-29 17:36:18      阅读:237      评论:0      收藏:0      [点我收藏+]
# 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))

 

Path Sum

原文:http://www.cnblogs.com/iois/p/3945130.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!