Skip to content

Commit 5d2c502

Browse files
authored
Create search_tree.md
1 parent 6047ed4 commit 5d2c502

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

medium/search_tree.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
## 不同的二叉搜索树
2+
给定一个整数 n,求以 1 ... n 为节点组成的二叉搜索树有多少种?
3+
4+
示例:
5+
6+
输入: 3
7+
输出: 5
8+
解释:
9+
给定 n = 3, 一共有 5 种不同结构的二叉搜索树:
10+
11+
1 3 3 2 1
12+
\ / / / \ \
13+
3 2 1 1 3 2
14+
/ / \ \
15+
2 1 2 3
16+
17+
## 解题思路
18+
[参考](https://www.cnblogs.com/ariel-dreamland/p/9159715.html)
19+
## 代码实现
20+
```C++
21+
class Solution {
22+
public:
23+
int numTrees(int n) {
24+
vector<int> dp(n+1,0);
25+
dp[0]=1;
26+
dp[1]=1;
27+
for(int i=2;i<=n;i++){
28+
for(int j=0;j<i;j++)
29+
{
30+
dp[i]+=dp[j]*dp[i-j-1];
31+
}
32+
}
33+
return dp[n];
34+
}
35+
};
36+
```

0 commit comments

Comments
 (0)