《LeetCode力扣练习》第96题 不同的二叉搜索树 Java
一、资源
-
题目:
给你一个整数 n ,求恰由 n 个节点组成且节点值从 1 到 n 互不相同的 二叉搜索树 有多少种?返回满足题意的二叉搜索树的种数。
示例 1:
输入:n = 3
输出:5示例 2:
输入:n = 1
输出:1提示:
1 <= n <= 19
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/unique-binary-search-trees
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 -
上代码(经过线上OJ测试)
/** * Created with IntelliJ IDEA. * * @author : DuZhenYang * @version : 2022.03.01 18:01:48 * description : */ public class LeetCode { public int numTrees(int n) { int[] G = new int[n + 1]; G[0] = 1; G[1] = 1; for (int i = 2; i <= n; ++i) { for (int j = 1; j <= i; ++j) { G[i] += G[j - 1] * G[i - j]; } } return G[n]; } }