首页 > 编程语言 > 详细

leetcode:Path Sum【Python版】

时间:2014-10-19 10:11:27      阅读:251      评论:0      收藏:0      [点我收藏+]

1、类中递归调用函数需要加self

# 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):
        ret = False
        if root == None:
            return ret
        sum -= root.val
        if sum==0 and root.left==None and root.right==None:
            ret = True
        return ret or self.hasPathSum(root.left,sum) or self.hasPathSum(root.right,sum)

 

leetcode:Path Sum【Python版】

原文:http://www.cnblogs.com/CheeseZH/p/4034291.html

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