The problem doesn’t ask for the trees, only the roots, then why spend time and space creating them?
def find_unique_trees(n):
results = []
for i in range(1, n + 1):
if i < 2:
left_variants = 1
else:
left_variants = len(find_unique_trees(i - 1))
if i > n - 2:
right_variants = 1
else:
right_variants = len(find_unique_trees(n - i))
results += [i] * (left_variants * right_variants)
return results
Type your question above this line.
Course: https://www.educative.io/collection/5668639101419520/5671464854355968
Lesson: https://www.educative.io/collection/page/5668639101419520/5671464854355968/6144747909414912