```
def generate_trees(n)
trees_helper([*1..n])
end
def trees_helper(arr)
return [nil] if arr.nil? || arr.empty?
return arr if arr.length == 1
combined = []
# different root (or pivot point) with each loop
for i in (arr.length-1).downto(0)
# construct left / right options
left = trees_helper(arr[0...i])
right = trees_helper(arr[i+1..-1])
# combine the options
left.each do |l|
right.each do |r|
node = TreeNode.new(arr[i])
node.left, node.right = l, r
combined << node
end
end
end
combined
end
```