[AlgoExpert] Number Of Binary Tree Topologies

Number Of Binary Tree Topologies

  • Time : O(n^2)
  • Space : O(n)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using namespace std;

int helper(vector<int>& dp, int n) {
if(dp[n] != -1) return dp[n];
dp[n] = 0;
for(int i = 0; i <= n - 1; i++)
dp[n] += helper(dp,i) * helper(dp, n - 1 - i);
return dp[n];
}

int numberOfBinaryTreeTopologies(int n) {
vector<int> dp(n + 1, -1);
dp[0] = dp[1] = 1;
return helper(dp, n);
}


Author: Song Hayoung
Link: https://songhayoung.github.io/2022/05/09/PS/AlgoExpert/number-of-binary-tree-topologies/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.