commit 95ca9de2bd17dc376d68d4700b0fe103a78179c2
parent df582d25708469baba523a9770c3a4704c145e78
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date: Sat, 5 Aug 2023 12:47:01 +0200
Daily Problem
Diffstat:
2 files changed, 22 insertions(+), 0 deletions(-)
diff --git a/Problems/0095.cpp b/Problems/0095.cpp
@@ -0,0 +1,21 @@
+class Solution {
+ vector<TreeNode *> dp[10][10];
+
+ vector<TreeNode *> rec(int n, int left, int right) {
+ vector<TreeNode *> &res = dp[left][right];
+ if (!res.empty()) return res;
+
+ for (int i = left; i <= right; i++) {
+ for (const auto &l : rec(n, left, i - 1)) {
+ for (const auto &r : rec(n, i + 1, right)) {
+ res.push_back(new TreeNode(i, l, r));
+ }
+ }
+ }
+ if (res.empty()) return {nullptr};
+ return res;
+ }
+
+public:
+ vector<TreeNode *> generateTrees(int n) { return rec(n, 1, n); }
+};
diff --git a/README.md b/README.md
@@ -110,6 +110,7 @@ for solving problems.
| 0092 | Medium | [Reverse Linked List II](Problems/0092.cpp) |
| 0093 | Medium | [Restore IP Addresses](Problems/0093.cpp) |
| 0094 | Easy | [Binary Tree Inorder Traversal](Problems/0094.cpp) |
+| 0095 | Medium | [Unique Binary Search Trees II](Problems/0095.cpp) |
| 0096 | Medium | [Unique Binary Search Trees](Problems/0096.cpp) |
| 0098 | Medium | [Validate Binary Search Tree](Problems/0098.cpp) |
| 0099 | Medium | [Recover Binary Search Tree](Problems/0099.cpp) |