码迷,mamicode.com
首页 > 其他好文 > 详细

Path Sum

时间:2014-08-29 17:36:18      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   ar   for   div   log   sp   

# 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

标签:style   blog   color   io   ar   for   div   log   sp   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!