/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<TreeNode> generateTrees(int n) {
if (n < 1) {
return new ArrayList<TreeNode>();
}
return subtreeHelper(1, n);
}
private List<TreeNode> subtreeHelper(int start, int end) {
List<TreeNode> rst = new ArrayList<TreeNode>();
if (start > end) {
rst.add(null);
return rst;
}
for (int i = start; i <= end; i++) {
List<TreeNode> leftSubtree = subtreeHelper(start, i - 1);
List<TreeNode> rightSubtree = subtreeHelper(i + 1, end);
for (TreeNode left : leftSubtree) {
for (TreeNode right : rightSubtree) {
TreeNode root = new TreeNode(i);
root.left = left;
root.right = right;
rst.add(root);
}
}
}
return rst;
}
}