solution.json 4.6 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
{
  "type": "code_options",
  "author": "https://github.com/begeekmyfriend/leetcode",
  "source": "solution.md",
  "exercise_id": "93f4a20c10584a4aa9a52d08e6f1df50",
  "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": "cpp"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ]
  ],
  "template": {
    "content": "#include <stdio.h>\n#include <stdlib.h>\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"
}