educative.io

Educative

Simpler solution with only one function, am I missing something?

def find_paths(root, sum):
  if not root:
    return []
  all_paths = []
  if not root.left and not root.right and root.val == sum:
    return [[root.val]]
  check_left = find_paths(root.left, sum - root.val)
  if check_left:
    all_paths += [[root.val] + sol for sol in check_left]
  check_right = find_paths(root.right, sum - root.val)
  if check_right:
    all_paths += [[root.val] + sol for sol in check_right]
  return all_paths

Type your question above this line.

Course: https://www.educative.io/collection/5668639101419520/5671464854355968
Lesson: https://www.educative.io/collection/page/5668639101419520/5671464854355968/5714315743068160

Hi @Andres_Parra,

No, you’re not missing anything. The logic is perfect!