{
  "type": "code_options",
  "author": "csdn.net",
  "source": "solution.md",
  "exercise_id": "5856e70cd3a5422dbad072f18477ed35",
  "keywords": "树,二叉搜索树,动态规划,回溯,二叉树",
  "title": "不同的二叉搜索树 II",
  "desc": [
    {
      "content": "\n<div class=\"notranslate\">\n<p>给你一个整数 <code>n</code> ，请你生成并返回所有由 <code>n</code> 个节点组成且节点值从 <code>1</code> 到 <code>n</code> 互不相同的不同\n<strong>二叉搜索树</strong><em> </em>。可以按 <strong>任意顺序</strong> 返回答案。\n</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p>&nbsp;</p>",
      "language": "markdown"
    },
    {
      "content": "\n<div class=\"original__bRMd\">\n<div>\n<p><strong>示例 1：</strong></p>\n<img style=\"width: 600px; height: 148px;\"\nsrc=\"https://assets.leetcode.com/uploads/2021/01/18/uniquebstn3.jpg\" alt=\"\">\n<pre><strong>输入：</strong>n = 3\n<strong>输出：</strong>[[1,null,2,null,3],[1,null,3,2],[2,1,3],[3,1,null,null,2],[3,2,null,1]]\n</pre>",
      "language": "markdown"
    },
    {
      "content": "\n<p><strong>示例 2：</strong></p>",
      "language": "markdown"
    },
    {
      "content": "\n<pre><strong>输入：</strong>n = 1\n<strong>输出：</strong>[[1]]\n</pre>",
      "language": "markdown"
    },
    {
      "content": "\n<p>&nbsp;</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p><strong>提示：</strong></p>",
      "language": "markdown"
    },
    {
      "content": "\n<ul>\n<li><code>1 &lt;= n &lt;= 8</code></li>\n</ul>\n</div>\n</div>\n</div>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "java"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ]
  ],
  "template": {
    "content": "class TreeNode {\n\tint val;\n\tTreeNode left;\n\tTreeNode right;\n\n\tTreeNode(int x) {\n\t\tval = x;\n\t}\n}\n\nclass Solution {\n\tpublic List<TreeNode> generateTrees(int n) {\n\n\t\tif (n == 0)\n\t\t\treturn new LinkedList<TreeNode>();\n\n\t\treturn generate_trees(1, n);\n\t}\n\n\tprivate LinkedList<TreeNode> generate_trees(int start, int end) {\n\t\tLinkedList<TreeNode> all_trees = new LinkedList<TreeNode>();\n\n\t\tif (start > end) {\n\t\t\tall_trees.add(null);\n\t\t\treturn all_trees;\n\t\t}\n\n\t\tfor (int i = start; i <= end; i++) {\n\t\t\tLinkedList<TreeNode> left_trees = generate_trees(start, i - 1);\n\t\t\tLinkedList<TreeNode> right_trees = generate_trees(i + 1, end);\n\n\t\t\tfor (TreeNode l : left_trees)\n\t\t\t\tfor (TreeNode r : right_trees) {\n\t\t\t\t\tTreeNode current_tree = new TreeNode(i);\n\t\t\t\t\tcurrent_tree.left = l;\n\t\t\t\t\tcurrent_tree.right = r;\n\t\t\t\t\tall_trees.add(current_tree);\n\t\t\t\t}\n\t\t}\n\n\t\treturn all_trees;\n\t}\n}",
    "language": "java"
  },
  "node_id": "dailycode-b97a45db1664450db39afacc504f65c7",
  "license": "csdn.net",
  "created_at": 1637894159,
  "topic_link": "https://bbs.csdn.net/topics/600471004"
}