def count_paths(root, S):
return count_paths_helper(root, S)
def count_paths_helper(root, S):
if root is None: # undefined null node
return 0
if root.val == S: # if current node = S and no children nodes used
return 1
# cases where we use children nodes
path_sum = 0
for target_sum in [S, S - root.val]:
path_sum += count_paths_helper(root.left, target_sum)
path_sum += count_paths_helper(root.right, target_sum)
return path_sum
Type your question above this line.
Course: https://www.educative.io/collection/5668639101419520/5671464854355968
Lesson: https://www.educative.io/collection/page/5668639101419520/5671464854355968/5658962204557312