Leetcode 112. Path Sum

Mingyu Jeon·2020년 5월 2일
0
post-thumbnail

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def hasPathSum(self, root: TreeNode, sum: int) -> bool:
        self.ans = False
        def dfs(node, total):
            if not node: 
                return
            
            total -= node.val
            
            if not node.left and not node.right and total == 0:
                self.ans = True
            dfs(node.left, total)
            dfs(node.right, total)
            
            
        dfs(root, sum)
        return self.ans

https://leetcode.com/problems/path-sum/

0개의 댓글