mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 08:50:15 +08:00
添加 0096.不同的二叉搜索树.md C语言解法
This commit is contained in:
@ -227,7 +227,34 @@ const numTrees =(n) => {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
C:
|
||||||
|
```c
|
||||||
|
//开辟dp数组
|
||||||
|
int *initDP(int n) {
|
||||||
|
int *dp = (int *)malloc(sizeof(int) * (n + 1));
|
||||||
|
int i;
|
||||||
|
for(i = 0; i <= n; ++i)
|
||||||
|
dp[i] = 0;
|
||||||
|
return dp;
|
||||||
|
}
|
||||||
|
|
||||||
|
int numTrees(int n){
|
||||||
|
//开辟dp数组
|
||||||
|
int *dp = initDP(n);
|
||||||
|
//将dp[0]设为1
|
||||||
|
dp[0] = 1;
|
||||||
|
|
||||||
|
int i, j;
|
||||||
|
for(i = 1; i <= n; ++i) {
|
||||||
|
for(j = 1; j <= i; ++j) {
|
||||||
|
//递推公式:dp[i] = d[i] + 根为j时左子树种类个数 * 根为j时右子树种类个数
|
||||||
|
dp[i] += dp[j - 1] * dp[i - j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return dp[n];
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||||
|
Reference in New Issue
Block a user