From 8ef040b18985643d17ec7f0b8b5228e9cf8255e1 Mon Sep 17 00:00:00 2001 From: wu xiangkai Date: Fri, 31 Oct 2025 10:16:06 +0800 Subject: [PATCH] =?UTF-8?q?feat:=202025-10-31=E6=89=93=E5=8D=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 96.unique-binary-search-trees.java | 32 ++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 96.unique-binary-search-trees.java diff --git a/96.unique-binary-search-trees.java b/96.unique-binary-search-trees.java new file mode 100644 index 0000000..65ebde4 --- /dev/null +++ b/96.unique-binary-search-trees.java @@ -0,0 +1,32 @@ +/* + * @lc app=leetcode id=96 lang=java + * + * [96] Unique Binary Search Trees + */ + +// @lc code=start +class Solution { + /** + * dp[n] = dp[n-1] + dp[1]+dp[n-2] + * + * @param n + * @return + */ + public int numTrees(int n) { + int[] dp = new int[n+1]; + for(int i=1; i<=n; i++) { + dp[i] = 0; + } + dp[1] = 1; + for(int i=2; i<=n; i++) { + for(int j=0; j<=i-1;j++) { + int left = j>0?dp[j]:1; + int right = i-1-j>0?dp[i-1-j]:1; + dp[i] += left*right; + } + } + return dp[n]; + } +} +// @lc code=end +