码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode 96. 不同的二叉搜索树

时间:2019-12-01 19:08:33      阅读:76      评论:0      收藏:0      [点我收藏+]

标签:solution   class   n+1   bsp   i++   整数   new   多少   tco   

给定一个整数 n,求以 1 ... n 为节点组成的二叉搜索树有多少种?

示例:

输入: 3
输出: 5
解释:
给定 n = 3, 一共有 5 种不同结构的二叉搜索树:

1 3 3 2 1
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3

 1 class Solution {
 2     public int numTrees(int n) {
 3         int [] dp = new int[n+1];
 4         dp[0]=1;
 5         dp[1]=1;
 6         for(int i = 2 ; i <= n ;i++){
 7             int tmp = 0;
 8             for(int j=0;j<i;j++){
 9                 tmp = tmp + dp[j]*dp[i-j-1];
10             }
11             dp[i]=tmp;
12         }
13         return dp[n];
14     }
15 }

 

leetcode 96. 不同的二叉搜索树

标签:solution   class   n+1   bsp   i++   整数   new   多少   tco   

原文地址:https://www.cnblogs.com/gongzixiaobaibcy/p/11966949.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!