leetcode-112とのパス

パスleetcode-112の合計

件名の説明:

二分木および目的地与えられ、この経路上のすべてのノードは、ツリー経路にリーフノードにルートノードが存在するかどうかを判断し、目標値に等しいが追加され

class Solution:
    def hasPathSum(self, root: TreeNode, sum: int) -> bool:
        if not root:
            return False
        if not root.left and not root.right and sum==root.val:
            return True
        return self.hasPathSum(root.left,sum-root.val) or self.hasPathSum(root.right,sum-root.val)

おすすめ

転載: www.cnblogs.com/curtisxiao/p/11208022.html