-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuniqueBinarySearchTreesII.cpp
More file actions
40 lines (38 loc) · 1.24 KB
/
uniqueBinarySearchTreesII.cpp
File metadata and controls
40 lines (38 loc) · 1.24 KB
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
// Source: https://leetcode.com/problems/unique-binary-search-trees-ii/
// Author: Miao Zhang
// Date: 2021-01-15
/**
* 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:
vector<TreeNode*> generateTrees(int n) {
if (n == 0) return {};
return dfs(1, n);
}
vector<TreeNode*> dfs(int left, int right) {
if (left > right) return {nullptr};
vector<TreeNode*> res;
for (int i = left; i < right + 1; i++) {
vector<TreeNode*> left_nodes = dfs(left, i - 1);
vector<TreeNode*> right_nodes = dfs(i + 1, right);
for (auto left_node: left_nodes) {
for (auto right_node: right_nodes) {
TreeNode* root = new TreeNode(i);
res.push_back(root);
root->left = left_node;
root->right = right_node;
}
}
}
return res;
}
};