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)

 

相关文章:

  • 2021-06-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-09
  • 2021-10-12
  • 2021-06-01
  • 2021-06-05
猜你喜欢
  • 2022-01-20
  • 2021-06-10
  • 2022-12-23
  • 2022-02-04
  • 2021-08-24
  • 2021-11-21
  • 2021-10-22
相关资源
相似解决方案