educative.io

Is this cheating?

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

Hi, when we say return roots we mean a starting/reference point for that tree. If we have to return the root values only then it would be simply an arithmetic problem and nothing to do with bst. We have to create proper structured binary search trees and return roots as their reference points.