# 不同的二叉搜索树 II

<div class="notranslate">
    <p>给你一个整数 <code>n</code> ，请你生成并返回所有由 <code>n</code> 个节点组成且节点值从 <code>1</code> 到 <code>n</code> 互不相同的不同
        <strong>二叉搜索树</strong><em> </em>。可以按 <strong>任意顺序</strong> 返回答案。
    </p>

    <p>&nbsp;</p>

    <div class="original__bRMd">
        <div>
            <p><strong>示例 1：</strong></p>
            <img style="width: 600px; height: 148px;"
                src="https://assets.leetcode.com/uploads/2021/01/18/uniquebstn3.jpg" alt="">
            <pre><strong>输入：</strong>n = 3
<strong>输出：</strong>[[1,null,2,null,3],[1,null,3,2],[2,1,3],[3,1,null,null,2],[3,2,null,1]]
    </pre>

            <p><strong>示例 2：</strong></p>

            <pre><strong>输入：</strong>n = 1
<strong>输出：</strong>[[1]]
    </pre>

            <p>&nbsp;</p>

            <p><strong>提示：</strong></p>

            <ul>
                <li><code>1 &lt;= n &lt;= 8</code></li>
            </ul>
        </div>
    </div>
</div>

## template

```java
class TreeNode {
	int val;
	TreeNode left;
	TreeNode right;

	TreeNode(int x) {
		val = x;
	}
}

class Solution {
	public List<TreeNode> generateTrees(int n) {

		if (n == 0)
			return new LinkedList<TreeNode>();

		return generate_trees(1, n);
	}

	private LinkedList<TreeNode> generate_trees(int start, int end) {
		LinkedList<TreeNode> all_trees = new LinkedList<TreeNode>();

		if (start > end) {
			all_trees.add(null);
			return all_trees;
		}

		for (int i = start; i <= end; i++) {
			LinkedList<TreeNode> left_trees = generate_trees(start, i - 1);
			LinkedList<TreeNode> right_trees = generate_trees(i + 1, end);

			for (TreeNode l : left_trees)
				for (TreeNode r : right_trees) {
					TreeNode current_tree = new TreeNode(i);
					current_tree.left = l;
					current_tree.right = r;
					all_trees.add(current_tree);
				}
		}

		return all_trees;
	}
}
```

## 答案

```java

```

## 选项

### A

```java

```

### B

```java

```

### C

```java

```