aboutsummaryrefslogtreecommitdiff
path: root/unique_bst.cpp
blob: 7aefacdd33890a0cdbe40d72d6621a0807b6707b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "leetcode.h"

class Solution {
public:
  int numTrees(int n) {
    if (n == 1)
      return 1;
    vector<int> dp(n + 1);
    dp[0] = dp[1] = 1;
    for (int i = 2; i <= n; i++) {
      for (int j = 0; j < i; j++) {
        int k = i - 1 - j;
        dp[i] += (dp[j] * dp[k]);
      }
    }
    return dp[n];
  }
};

int main() {
  Solution obj;
  cout << obj.numTrees(3);
}