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奶酪博客园博客,原文链接:http://www.cnblogs.com/CheeseZH/p/4034291.html,如需转载请自行联系原作者

相关文章
|
4月前
|
存储 索引 Python
leetcode-350:两个数组的交集 II(python中Counter的用法,海象运算符:=)
leetcode-350:两个数组的交集 II(python中Counter的用法,海象运算符:=)
30 0
|
6天前
|
API Python
[AIGC] 使用Python刷LeetCode:常用API及技巧指南
[AIGC] 使用Python刷LeetCode:常用API及技巧指南
|
4月前
|
算法 索引 Python
leetcode-138:复制带随机指针的链表 (python中copy与deepcopy区别)
leetcode-138:复制带随机指针的链表 (python中copy与deepcopy区别)
33 0
|
5月前
|
开发者 索引 Python
【python刷题】LeetCode 2057E 值相等的最小索引(5种简单高效的解法)
【python刷题】LeetCode 2057E 值相等的最小索引(5种简单高效的解法)
26 0
|
11月前
|
Python
Python|leetcode-访问所有点的最小时间
Python|leetcode-访问所有点的最小时间
51 0
|
12月前
|
Python
Python|Leetcode《539》|最小时间差
Python|Leetcode《539》|最小时间差
|
12月前
|
Python
Python|Leetcode《1220》|统计元音字母序列的数目
Python|Leetcode《1220》|统计元音字母序列的数目
|
12月前
|
Python
Python|Leetcode《334》|递增的三元子序列
Python|Leetcode《334》|递增的三元子序列
|
12月前
|
算法 Python
Python|Leetcode《306》|累加数
Python|Leetcode《306》|累加数
|
12月前
|
存储 Unix Python
Python|Leetcode《71》|简化路径
Python|Leetcode《71》|简化路径