动态规划,根据二叉树的根节点将原问题划分为两个子问题。
代码:
1 int numTrees(int n) { 2 vector<int> num(n + 1, 0); 3 4 num[0] = 1; 5 for (int i = 1; i <= n; i++) 6 for (int j = 1; j <= i; j++) 7 num[i] += num[j - 1] * num[i - j]; 8 9 return num[n]; 10 }
动态规划,根据二叉树的根节点将原问题划分为两个子问题。
代码:
1 int numTrees(int n) { 2 vector<int> num(n + 1, 0); 3 4 num[0] = 1; 5 for (int i = 1; i <= n; i++) 6 for (int j = 1; j <= i; j++) 7 num[i] += num[j - 1] * num[i - j]; 8 9 return num[n]; 10 }
转载于:https://www.cnblogs.com/boring09/p/4259933.html