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