Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

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)
Source by eolnuha.com #
 
PREVIOUS NEXT
Tagged: #path #sum #python
ADD COMMENT
Topic
Name
8+1 =