leetcode:Path Sum【Python版】
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)
找我内推: 字节跳动各种岗位
作者:
ZH奶酪(张贺)
邮箱:
cheesezh@qq.com
出处:
http://www.cnblogs.com/CheeseZH/
*
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。