0096.cpp (273B)
1 class Solution { 2 public: 3 int numTrees(int n) { 4 vector<int> dp(n + 1); 5 dp[0] = dp[1] = 1; 6 7 for (int i = 2; i <= n; i++) 8 for (int j = 1; j <= i; j++) 9 dp[i] += dp[j - 1] * dp[i - j]; 10 return dp.back(); 11 } 12 };