YSMull
<-- algorithm



原题链接
class Solution {
    public List<TreeNode> generateTrees(int n) {
        if (n == 0) {
            return Collections.emptyList();
        }
        return generateTrees(1, n);
    }
    // 这个函数的语义本来就是从零构建一个树的集合,所以递归时得到的都是新创建的
    public List<TreeNode> generateTrees(int left, int right) {
        List<TreeNode> res = new ArrayList<>();
        if (left > right) return Collections.singletonList(null);
        for (int i = left; i <= right; i++) {
            List<TreeNode> leftTree = generateTrees(left, i - 1);
            List<TreeNode> rightTree = generateTrees(i + 1, right);
            for (TreeNode l : leftTree) {
                for (TreeNode r : rightTree) {
                    TreeNode root = new TreeNode(i);
                    root.left = l;
                    root.right = r;
                    res.add(root);
                }
            }
        }
        return res;
    }
}