-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathAll Possible Full Binary Trees.cpp
65 lines (49 loc) · 1.79 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
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
/*
Solution by Rahul Surana
***********************************************************
Given an integer n, return a list of all possible full binary trees with n nodes.
Each node of each tree in the answer must have Node.val == 0.
Each element of the answer is the root node of one possible tree.
You may return the final list of trees in any order.
A full binary tree is a binary tree where each node has exactly 0 or 2 children.
***********************************************************
*/
#include <bits/stdc++.h>
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
map<int,vector<TreeNode*>> m;
vector<TreeNode*> aPFBT(int n) {
if(n==1) return vector<TreeNode*>(1,new TreeNode(0));
if(m.find(n) != m.end()) return m[n];
vector<TreeNode*> ans;
// if(n%2) return ans;
for(int i = 1; i < n; i++){
int l = i,r = n-1-i;
for(auto left : aPFBT(l)){
for(auto right : aPFBT(r)){
TreeNode* root = new TreeNode(0);
root->left = left;
root->right = right;
ans.push_back(root);
}
}
}
// m[n] = new vector(ans.begin(),ans.end());
return m[n] = ans;
}
vector<TreeNode*> allPossibleFBT(int n) {
if(n%2==0) return vector<TreeNode*>();
return aPFBT(n);
}
};