blob: 438d9b77e6241fee4d5f82718f07c7eda2ad4442 (
plain)
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
|
// 95. Unique Binary Search Trees II
#include "leetcode.h"
/*
* given an integer 'n', return all the structurally unique BST's which has
* exactly 'n' nodes of unique values from 1 to 'n'. return the answer in any
* order.
*/
class Solution {
vector<TreeNode *> build(int from, int to) {
vector<TreeNode *> res;
if (to - from < 0)
res.push_back(0);
if (to - from == 0)
res.push_back(new TreeNode(from));
if (to - from > 0)
for (int i = 0; i <= to; i++) {
vector<TreeNode *> left = build(from, i - 1);
vector<TreeNode *> right = build(i + 1, to);
for (int j = 0; j < left.size(); j++)
for (int k = 0; k < right.size(); k++) {
TreeNode *height = new TreeNode(i);
height->left = left[j];
height->right = right[k];
res.push_back(height);
}
}
return res;
}
public:
vector<TreeNode *> generateTrees(int n) { return build(1, n); }
};
|