-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path095_unique_binary_search_trees_ii.cpp
87 lines (73 loc) · 2.21 KB
/
095_unique_binary_search_trees_ii.cpp
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
77
78
79
80
81
82
83
84
85
86
87
/*
Unique Binary Search Trees II
URL: https://leetcode.com/problems/unique-binary-search-trees-ii
Tags: ['dynamic-programming', 'tree']
___
Given an integer n , generate all structurally unique BST 's (binary search
trees) that store values 1 ... n.
Example:
Input: 3
Output:
[
[1,null,3,2],
[3,2,null,1],
[3,1,null,null,2],
[2,1,3],
[1,null,2,null,3]
]
Explanation:
The above output corresponds to the 5 unique BST 's shown below:
1 3 3 2 1
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3
*/
#include "test.h"
using namespace leetcode;
using std::vector;
namespace unique_binary_search_trees_ii {
inline namespace v1 {
/*
设 `F(begin, end)` 为解:
1. 当 `begin == end`, 则只有一种方案.
2. 否则 `F(begin, end) = [F(begin, i) * F(i+1, end) for i in range(begin, end)]`
*/
class Solution {
public:
vector<TreeNode*> generateTrees(int n) { return step(1, n); }
private:
vector<TreeNode*> step(int begin, int end) {
vector<TreeNode*> result;
for (auto i = begin; i <= end; i++) {
// 计算所有左边可能的结果
auto lefts = step(begin, i - 1);
if (lefts.empty()) { // 没有结果时可以选择为空
lefts.push_back(nullptr);
}
// 计算所有右边可能的结果
auto rights = step(i + 1, end);
if (rights.empty()) { // 没有结果时可以选择为空
rights.push_back(nullptr);
}
for (auto l : lefts) {
for (auto r : rights) {
auto node = new TreeNode(i);
node->left = l;
node->right = r;
result.push_back(node);
}
}
}
return result;
}
};
} // namespace v1
TEST_CASE("Unique Binary Search Trees II") {
// TODO 完善 test case
TEST_SOLUTION(generateTrees, v1) {
CHECK(generateTrees(3).size() == 5);
BENCHMARK("") { return generateTrees(3); };
};
}
} // namespace unique_binary_search_trees_ii