-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy path894-所有可能的满二叉树.cpp
More file actions
36 lines (36 loc) · 948 Bytes
/
894-所有可能的满二叉树.cpp
File metadata and controls
36 lines (36 loc) · 948 Bytes
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
/**
* 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) {
vector<TreeNode*> res;
if(N == 1){
TreeNode* node = new TreeNode(0);
res.push_back(node);
return res;
}
N--;
for(int i=1; i<=N; i += 2){
//递归构造左右子树
vector<TreeNode*> L = allPossibleFBT(i);
vector<TreeNode*> R = allPossibleFBT(N-i);
//组合
for(auto l : L){
for(auto r : R){
TreeNode* node = new TreeNode(0);
node->left = l;
node->right = r;
res.push_back(node);
}
}
}
return res;
}
};