forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
all-possible-full-binary-trees.cpp
39 lines (37 loc) · 1.18 KB
/
all-possible-full-binary-trees.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
// Time: O(n * 4^n / n^(3/2)) ~= sum of Catalan numbers from 1 .. N
// Space: O(n * 4^n / n^(3/2)) ~= sum of Catalan numbers from 1 .. N
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<TreeNode*> allPossibleFBT(int N) {
if (!memo_.count(N)) {
vector<TreeNode*> result;
if (N == 1) {
result.emplace_back(new TreeNode(0));
} else if (N % 2 == 1) {
for (int i = 0; i < N; ++i) {
for (const auto& left: allPossibleFBT(i)) {
for (const auto& right: allPossibleFBT(N - 1 - i)) {
auto node = new TreeNode(0);
node->left = left;
node->right = right;
result.emplace_back(node);
}
}
}
}
memo_[N] = move(result);
}
return memo_[N];
}
private:
unordered_map<int, vector<TreeNode*>> memo_;
};