leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0095.cpp (615B)


      1 class Solution {
      2     vector<TreeNode *> dp[10][10];
      3 
      4     vector<TreeNode *> rec(int n, int left, int right) {
      5         vector<TreeNode *> &res = dp[left][right];
      6         if (!res.empty()) return res;
      7 
      8         for (int i = left; i <= right; i++) {
      9             for (const auto &l : rec(n, left, i - 1)) {
     10                 for (const auto &r : rec(n, i + 1, right)) {
     11                     res.push_back(new TreeNode(i, l, r));
     12                 }
     13             }
     14         }
     15         if (res.empty()) return {nullptr};
     16         return res;
     17     }
     18 
     19   public:
     20     vector<TreeNode *> generateTrees(int n) { return rec(n, 1, n); }
     21 };