[InterviewBit] Unique Binary Search Trees II

Unique Binary Search Trees II

  • Time :
  • Space :
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);
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/09/13/PS/interviewbit/unique-binary-search-trees-ii/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.