solution.md 5.7 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
# 不同的二叉搜索树 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>

每日一练社区's avatar
每日一练社区 已提交
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
以下程序实现了这一功能,请你填补空白处内容:

```cpp
#include <stdio.h>
#include <stdlib.h>
struct TreeNode
{
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
};
static struct TreeNode *dfs(int low, int high, int *count)
{
	int i, j, k;
	if (low > high)
	{
		*count = 0;
		return NULL;
	}
	else if (low == high)
	{
		struct TreeNode *node = malloc(sizeof(*node));
		node->val = low;
		node->left = NULL;
		node->right = NULL;
		*count = 1;
		return node;
	}
	else
	{
		*count = 0;
		int capacity = 5;
		struct TreeNode *roots = malloc(capacity * sizeof(struct TreeNode));
		for (i = low; i <= high; i++)
		{
			int left_cnt, right_cnt;
ToTensor's avatar
ToTensor 已提交
72
			________________________________;
每日一练社区's avatar
每日一练社区 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
			if (left_cnt == 0)
				left_cnt = 1;
			if (right_cnt == 0)
				right_cnt = 1;
			if (*count + (left_cnt * right_cnt) >= capacity)
			{
				capacity *= 2;
				capacity += left_cnt * right_cnt;
				roots = realloc(roots, capacity * sizeof(struct TreeNode));
			}
			for (j = 0; j < left_cnt; j++)
			{
				for (k = 0; k < right_cnt; k++)
				{
					roots[*count].val = i;
					roots[*count].left = left_subs == NULL ? NULL : &left_subs[j];
					roots[*count].right = right_subs == NULL ? NULL : &right_subs[k];
					(*count)++;
				}
			}
		}
		return roots;
	}
}
static struct TreeNode **generateTrees(int n, int *returnSize)
{
	int i, count = 0;
	struct TreeNode *roots = dfs(1, n, &count);
	struct TreeNode **results = malloc(count * sizeof(struct TreeNode *));
	for (i = 0; i < count; i++)
	{
		results[i] = &roots[i];
	}
	*returnSize = count;
	return results;
}
static void dump(struct TreeNode *node)
{
	printf("%d ", node->val);
	if (node->left != NULL)
	{
		dump(node->left);
	}
	else
	{
		printf("# ");
	}
	if (node->right != NULL)
	{
		dump(node->right);
	}
	else
	{
		printf("# ");
	}
}
int main(int argc, char **argv)
{
	if (argc != 2)
	{
		fprintf(stderr, "Usage: ./test n\n");
		exit(-1);
	}
	int i, count = 0;
	struct TreeNode **results = generateTrees(atoi(argv[1]), &count);
	for (i = 0; i < count; i++)
	{
		dump(results[i]);
		printf("\n");
	}
	return 0;
}
```

每日一练社区's avatar
每日一练社区 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
## template

```cpp
#include <stdio.h>
#include <stdlib.h>
struct TreeNode
{
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
};
static struct TreeNode *dfs(int low, int high, int *count)
{
	int i, j, k;
	if (low > high)
	{
		*count = 0;
		return NULL;
	}
	else if (low == high)
	{
		struct TreeNode *node = malloc(sizeof(*node));
		node->val = low;
		node->left = NULL;
		node->right = NULL;
		*count = 1;
		return node;
	}
	else
	{
		*count = 0;
		int capacity = 5;
		struct TreeNode *roots = malloc(capacity * sizeof(struct TreeNode));
		for (i = low; i <= high; i++)
		{
			int left_cnt, right_cnt;
			struct TreeNode *left_subs = dfs(low, i - 1, &left_cnt);
			struct TreeNode *right_subs = dfs(i + 1, high, &right_cnt);
			if (left_cnt == 0)
				left_cnt = 1;
			if (right_cnt == 0)
				right_cnt = 1;
			if (*count + (left_cnt * right_cnt) >= capacity)
			{
				capacity *= 2;
				capacity += left_cnt * right_cnt;
				roots = realloc(roots, capacity * sizeof(struct TreeNode));
			}
			for (j = 0; j < left_cnt; j++)
			{
				for (k = 0; k < right_cnt; k++)
				{
					roots[*count].val = i;
					roots[*count].left = left_subs == NULL ? NULL : &left_subs[j];
					roots[*count].right = right_subs == NULL ? NULL : &right_subs[k];
					(*count)++;
				}
			}
		}
		return roots;
	}
}
static struct TreeNode **generateTrees(int n, int *returnSize)
{
	int i, count = 0;
	struct TreeNode *roots = dfs(1, n, &count);
	struct TreeNode **results = malloc(count * sizeof(struct TreeNode *));
	for (i = 0; i < count; i++)
	{
		results[i] = &roots[i];
	}
	*returnSize = count;
	return results;
}
static void dump(struct TreeNode *node)
{
	printf("%d ", node->val);
	if (node->left != NULL)
	{
		dump(node->left);
	}
	else
	{
		printf("# ");
	}
	if (node->right != NULL)
	{
		dump(node->right);
	}
	else
	{
		printf("# ");
	}
}
int main(int argc, char **argv)
{
	if (argc != 2)
	{
		fprintf(stderr, "Usage: ./test n\n");
		exit(-1);
	}
	int i, count = 0;
	struct TreeNode **results = generateTrees(atoi(argv[1]), &count);
	for (i = 0; i < count; i++)
	{
		dump(results[i]);
		printf("\n");
	}
	return 0;
}
```

## 答案

```cpp
每日一练社区's avatar
每日一练社区 已提交
262 263
struct TreeNode *left_subs = dfs(low, i - 1, &left_cnt);
struct TreeNode *right_subs = dfs(i + 1, high, &right_cnt);
每日一练社区's avatar
每日一练社区 已提交
264 265 266 267 268 269 270
```

## 选项

### A

```cpp
每日一练社区's avatar
每日一练社区 已提交
271 272
struct TreeNode *left_subs = dfs(low, i + 1, &left_cnt);
struct TreeNode *right_subs = dfs(i + 1, high, &right_cnt);
每日一练社区's avatar
每日一练社区 已提交
273 274 275 276 277
```

### B

```cpp
每日一练社区's avatar
每日一练社区 已提交
278 279
struct TreeNode *left_subs = dfs(low, i - 1, &left_cnt);
struct TreeNode *right_subs = dfs(i - 1, high, &right_cnt);
每日一练社区's avatar
每日一练社区 已提交
280 281 282 283 284
```

### C

```cpp
每日一练社区's avatar
每日一练社区 已提交
285 286
struct TreeNode *left_subs = dfs(high, i - 1, &left_cnt);
struct TreeNode *right_subs = dfs(i + 1, low, &right_cnt);
每日一练社区's avatar
每日一练社区 已提交
287
```