Unique Binary Search Trees II
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| int dp[20]; int helper(int n) { if(n <= 1) return 1; if(dp[n]) return dp[n]; int& res = dp[n]; for(int i = 1; i <= n; i++) { res += helper(i - 1) * helper(n - i); } return res; } int Solution::numTrees(int A) { return helper(A); }
|