Path Sum
Tree DFS
Last updated
Tree DFS
Last updated
class Solution:
def hasPathSum(self, root: TreeNode, sum: int) -> bool:
if root is None:
return
if root.left == None and root.right == None and root.val == sum:
return True
return self.hasPathSum(root.left, sum-root.val) or self.hasPathSum(root.right, sum-root.val)
#Time: O(n) where 'n' is the total number of leaf nodes
#Space: O(n) because the space is the recursion stack
#and worst case is every node has one child