path sum with python

52

path sum with python -

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.left = left
class Solution:
    def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
        if root == None:
            return False

        remain = targetSum - root.val

        if remain == 0 and root.left == None and root.right == None:
            return True

        return self.hasPathSum(root.left, remain) or self.hasPathSum(root.right, remain)

Comments

Submit
0 Comments