{
  "type": "code_options",
  "author": "https://github.com/qiyuangong/leetcode",
  "source": "solution.md",
  "exercise_id": "55102e8ea1ca4b7abadbc1595fcef085",
  "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": "python"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "python"
      }
    ],
    [
      {
        "content": "",
        "language": "python"
      }
    ],
    [
      {
        "content": "",
        "language": "python"
      }
    ]
  ],
  "template": {
    "content": "class TreeNode(object):\n\tdef __init__(self, x):\n\t\tself.val = x\n\t\tself.left = None\n\t\tself.right = None\n\tdef to_list(self, count):\n\t\tqueue = []\n\t\tqueue.append(self)\n\t\tresult = []\n\t\twhile len(queue) > 0:\n\t\t\tif count == 0:\n\t\t\t\tbreak\n\t\t\tnode = queue.pop(0)\n\t\t\tif node is None:\n\t\t\t\tresult.append('null')\n\t\t\telse:\n\t\t\t\tcount -= 1\n\t\t\t\tresult.append(node.val)\n\t\t\t\tqueue.append(node.left)\n\t\t\t\tqueue.append(node.right)\n\t\treturn result\nclass Solution(object):\n\tdef generateTrees(self, n):\n\t\t\"\"\"\n\t\t:type n: int\n\t\t:rtype: List[TreeNode]\n\t\t\"\"\"\n\t\tif n == 0:\n\t\t\treturn []\n\t\treturn self.get_trees(1, n)\n\tdef get_trees_impl(self, start, end):\n\t\ttrees = []\n\t\tif start > end:\n\t\t\ttrees.append(None)\n\t\t\treturn trees\n\t\tfor i in range(start, end + 1):\n\t\t\tlefts = self.get_trees_impl(start, i - 1)\n\t\t\trights = self.get_trees_impl(i + 1, end)\n\t\t\tfor j in range(len(lefts)):\n\t\t\t\tfor k in range(len(rights)):\n\t\t\t\t\troot = TreeNode(i)\n\t\t\t\t\troot.left = lefts[j]\n\t\t\t\t\troot.right = rights[k]\n\t\t\t\t\ttrees.append(root)\n\t\treturn trees\n\tdef get_trees(self, start, end):\n\t\ttrees = self.get_trees_impl(start, end)\n\t\tresults = []\n\t\tfor tree in trees:\n\t\t\tif tree is None:\n\t\t\t\tresults.append([])\n\t\t\telse:\n\t\t\t\tresults.append(tree.to_list(end))\n\t\treturn results\n# %%\ns = Solution()\nprint(s.generateTrees(n=3))",
    "language": "python"
  },
  "node_id": "dailycode-527f1c40eca54c19be59eb9d93305dc3",
  "license": "csdn.net",
  "created_at": 1637894159,
  "topic_link": "https://bbs.csdn.net/topics/600471004"
}