{ "type": "code_options", "author": "https://github.com/begeekmyfriend/leetcode", "source": "solution.md", "exercise_id": "93f4a20c10584a4aa9a52d08e6f1df50", "keywords": "树,二叉搜索树,动态规划,回溯,二叉树", "title": "不同的二叉搜索树 II", "desc": [ { "content": "\n
\n

给你一个整数 n ,请你生成并返回所有由 n 个节点组成且节点值从 1n 互不相同的不同\n二叉搜索树 。可以按 任意顺序 返回答案。\n

", "language": "markdown" }, { "content": "\n

 

", "language": "markdown" }, { "content": "\n
\n
\n

示例 1:

\n\"\"\n
输入:n = 3\n输出:[[1,null,2,null,3],[1,null,3,2],[2,1,3],[3,1,null,null,2],[3,2,null,1]]\n
", "language": "markdown" }, { "content": "\n

示例 2:

", "language": "markdown" }, { "content": "\n
输入:n = 1\n输出:[[1]]\n
", "language": "markdown" }, { "content": "\n

 

", "language": "markdown" }, { "content": "\n

提示:

", "language": "markdown" }, { "content": "\n
    \n
  • 1 <= n <= 8
  • \n
\n
\n
\n
", "language": "markdown" } ], "answer": [ { "content": "", "language": "cpp" } ], "prepared": [ [ { "content": "", "language": "cpp" } ], [ { "content": "", "language": "cpp" } ], [ { "content": "", "language": "cpp" } ] ], "template": { "content": "#include \n#include \nstruct TreeNode\n{\n\tint val;\n\tstruct TreeNode *left;\n\tstruct TreeNode *right;\n};\nstatic struct TreeNode *dfs(int low, int high, int *count)\n{\n\tint i, j, k;\n\tif (low > high)\n\t{\n\t\t*count = 0;\n\t\treturn NULL;\n\t}\n\telse if (low == high)\n\t{\n\t\tstruct TreeNode *node = malloc(sizeof(*node));\n\t\tnode->val = low;\n\t\tnode->left = NULL;\n\t\tnode->right = NULL;\n\t\t*count = 1;\n\t\treturn node;\n\t}\n\telse\n\t{\n\t\t*count = 0;\n\t\tint capacity = 5;\n\t\tstruct TreeNode *roots = malloc(capacity * sizeof(struct TreeNode));\n\t\tfor (i = low; i <= high; i++)\n\t\t{\n\t\t\tint left_cnt, right_cnt;\n\t\t\tstruct TreeNode *left_subs = dfs(low, i - 1, &left_cnt);\n\t\t\tstruct TreeNode *right_subs = dfs(i + 1, high, &right_cnt);\n\t\t\tif (left_cnt == 0)\n\t\t\t\tleft_cnt = 1;\n\t\t\tif (right_cnt == 0)\n\t\t\t\tright_cnt = 1;\n\t\t\tif (*count + (left_cnt * right_cnt) >= capacity)\n\t\t\t{\n\t\t\t\tcapacity *= 2;\n\t\t\t\tcapacity += left_cnt * right_cnt;\n\t\t\t\troots = realloc(roots, capacity * sizeof(struct TreeNode));\n\t\t\t}\n\t\t\tfor (j = 0; j < left_cnt; j++)\n\t\t\t{\n\t\t\t\tfor (k = 0; k < right_cnt; k++)\n\t\t\t\t{\n\t\t\t\t\troots[*count].val = i;\n\t\t\t\t\troots[*count].left = left_subs == NULL ? NULL : &left_subs[j];\n\t\t\t\t\troots[*count].right = right_subs == NULL ? NULL : &right_subs[k];\n\t\t\t\t\t(*count)++;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn roots;\n\t}\n}\nstatic struct TreeNode **generateTrees(int n, int *returnSize)\n{\n\tint i, count = 0;\n\tstruct TreeNode *roots = dfs(1, n, &count);\n\tstruct TreeNode **results = malloc(count * sizeof(struct TreeNode *));\n\tfor (i = 0; i < count; i++)\n\t{\n\t\tresults[i] = &roots[i];\n\t}\n\t*returnSize = count;\n\treturn results;\n}\nstatic void dump(struct TreeNode *node)\n{\n\tprintf(\"%d \", node->val);\n\tif (node->left != NULL)\n\t{\n\t\tdump(node->left);\n\t}\n\telse\n\t{\n\t\tprintf(\"# \");\n\t}\n\tif (node->right != NULL)\n\t{\n\t\tdump(node->right);\n\t}\n\telse\n\t{\n\t\tprintf(\"# \");\n\t}\n}\nint main(int argc, char **argv)\n{\n\tif (argc != 2)\n\t{\n\t\tfprintf(stderr, \"Usage: ./test n\\n\");\n\t\texit(-1);\n\t}\n\tint i, count = 0;\n\tstruct TreeNode **results = generateTrees(atoi(argv[1]), &count);\n\tfor (i = 0; i < count; i++)\n\t{\n\t\tdump(results[i]);\n\t\tprintf(\"\\n\");\n\t}\n\treturn 0;\n}", "language": "cpp" }, "node_id": "dailycode-b1ac676f141f4700bf7a6461416bf409", "license": "csdn.net", "created_at": 1637894158, "topic_link": "https://bbs.csdn.net/topics/600471004" }