class Solution {
List<TreeNode> list;
public List<TreeNode> generateTrees(int n) {
list = new ArrayList<>();
return dfs(1,n);
}
List<TreeNode> dfs(int l,int r){
List<TreeNode> arr = new ArrayList<>();
if(l == r){
TreeNode node = new TreeNode(l);
arr.add(node);
return arr;
}
if(l > r){
arr.add(null);
return arr;
}
for(int i = l;i <= r;i++){
List<TreeNode> left = dfs(l,i - 1);
List<TreeNode> right = dfs(i + 1,r);
for(TreeNode a : left){
for(TreeNode b : right){
TreeNode cur = new TreeNode(i);
cur.left = a;
cur.right = b;
arr.add(cur);
}
}
}
return arr;
}
}