{ "type": "code_options", "author": "csdn.net", "source": "solution.md", "exercise_id": "117c1490fa86467bb299835aa5d0b3e4", "keywords": "树,二叉搜索树,数学,动态规划,二叉树", "title": "不同的二叉搜索树", "desc": [ { "content": "\n
\n

给你一个整数 n ,求恰由 n 个节点组成且节点值从 1n 互不相同的 二叉搜索树\n有多少种?返回满足题意的二叉搜索树的种数。

", "language": "markdown" }, { "content": "\n

 

", "language": "markdown" }, { "content": "\n

示例 1:

\n\n
输入:n = 3\n输出:5\n
", "language": "markdown" }, { "content": "\n

示例 2:

", "language": "markdown" }, { "content": "\n
输入:n = 1\n输出:1\n
", "language": "markdown" }, { "content": "\n

 

", "language": "markdown" }, { "content": "\n

提示:

", "language": "markdown" }, { "content": "\n\n
", "language": "markdown" } ], "answer": [ { "content": "", "language": "java" } ], "prepared": [ [ { "content": "", "language": "java" } ], [ { "content": "", "language": "java" } ], [ { "content": "", "language": "java" } ] ], "template": { "content": "class Solution {\n\tpublic int numTrees(int n) {\n\t\tif (n < 2) {\n\t\t\treturn 1;\n\t\t}\n\n\t\tint[] count = new int[n + 1];\n\t\tcount[0] = 1;\n\t\tcount[1] = 1;\n\t\tfor (int i = 2; i <= n; i++) {\n\t\t\tint sum = 0;\n\t\t\tfor (int root = 1; root <= i; root++) {\n\t\t\t\tsum = sum + count[root - 1] * count[i - root];\n\t\t\t}\n\t\t\tcount[i] = sum;\n\t\t}\n\n\t\treturn count[n];\n\t}\n}\n", "language": "java" }, "node_id": "dailycode-2b9e60620fe645f883040218b22a8d76", "license": "csdn.net", "created_at": 1637894158, "topic_link": "https://bbs.csdn.net/topics/600469926" }