forked from lzl124631x/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s1.cpp
25 lines (25 loc) · 785 Bytes
/
s1.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
// OJ: https://leetcode.com/problems/all-possible-full-binary-trees/
// Author: github.com/lzl124631x
// Time: O(2^N)
// Space: O(2^N)
class Solution {
public:
vector<TreeNode*> allPossibleFBT(int N) {
if (N % 2 == 0) return {};
if (N == 1) return { new TreeNode(0) };
vector<TreeNode*> ans;
for (int i = 1; i <= N - 2; i += 2) {
auto lefts = allPossibleFBT(i);
auto rights = allPossibleFBT(N - i - 1);
for (auto left : lefts) {
for (auto right : rights) {
auto root = new TreeNode(0);
root->left = left;
root->right = right;
ans.push_back(root);
}
}
}
return ans;
}
};