Skip to content

Commit d46ef3a

Browse files
committed
day 2
1 parent 40fecce commit d46ef3a

File tree

2 files changed

+72
-4
lines changed

2 files changed

+72
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
Unique Binary Search Trees II
3+
=============================
4+
5+
Given an integer n, return all the structurally unique BST's (binary search trees), which has exactly n nodes of unique values from 1 to n. Return the answer in any order.
6+
7+
Example 1:
8+
Input: n = 3
9+
Output: [[1,null,2,null,3],[1,null,3,2],[2,1,3],[3,1,null,null,2],[3,2,null,1]]
10+
11+
Example 2:
12+
Input: n = 1
13+
Output: [[1]]
14+
15+
Constraints:
16+
1 <= n <= 8
17+
*/
18+
19+
/**
20+
* Definition for a binary tree node.
21+
* struct TreeNode {
22+
* int val;
23+
* TreeNode *left;
24+
* TreeNode *right;
25+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
26+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
27+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
28+
* };
29+
*/
30+
31+
class Solution
32+
{
33+
public:
34+
vector<TreeNode *> dfs(int start, int end)
35+
{
36+
if (start > end)
37+
return {NULL};
38+
if (start == end)
39+
return {new TreeNode(start)};
40+
41+
vector<TreeNode *> ans;
42+
43+
for (int i = start; i <= end; ++i)
44+
{
45+
auto left = dfs(start, i - 1);
46+
auto right = dfs(i + 1, end);
47+
48+
for (auto &l : left)
49+
{
50+
for (auto &r : right)
51+
{
52+
TreeNode *root = new TreeNode(i);
53+
root->left = l;
54+
root->right = r;
55+
ans.push_back(root);
56+
}
57+
}
58+
}
59+
60+
return ans;
61+
}
62+
63+
vector<TreeNode *> generateTrees(int n)
64+
{
65+
return dfs(1, n);
66+
}
67+
};
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# September 2021 LeetCoding Challenge
22

3-
| Day | Question Links | Solutions |
4-
| :-: | :--------------------------------------------------------------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------: |
5-
| | []() | [cpp](./.cpp) |
6-
| 1. | [Array Nesting](https://leetcode.com/explore/featured/card/september-leetcoding-challenge-2021/636/week-1-september-1st-september-7th/3960/) | [cpp](./01.%20Array%20Nesting.cpp) |
3+
| Day | Question Links | Solutions |
4+
| :-: | :------------------------------------------------------------------------------------------------------------------------------------------------------------ | :------------------------------------------------------: |
5+
| | []() | [cpp](./.cpp) |
6+
| 1. | [Array Nesting](https://leetcode.com/explore/featured/card/september-leetcoding-challenge-2021/636/week-1-september-1st-september-7th/3960/) | [cpp](./01.%20Array%20Nesting.cpp) |
7+
| 2. | [Unique Binary Search Trees II](https://leetcode.com/explore/challenge/card/september-leetcoding-challenge-2021/636/week-1-september-1st-september-7th/3961/) | [cpp](./02.%20Unique%20Binary%20Search%20Trees%20II.cpp) |

0 commit comments

Comments
 (0)