We can omit passing right and left child into a recursive call if we know they are both None
.
def has_path(root, sum):
if root is None:
return False
# we are at a leaf node thus we need to check if we reached the sum
if root.left is None and root.right is None:
return sum == root.val
return has_path(root.left, sum - root.val) or has_path(root.right, sum - root.val)
Type your question above this line.
Course: https://www.educative.io/collection/5668639101419520/5671464854355968
Lesson: https://www.educative.io/collection/page/5668639101419520/5671464854355968/5642684278505472