diff --git a/alg.md b/alg.md index 28a29842143c0cd1cd6e8c4684d6e04d8e664a13..610d2b8d3fe3d9ebd48aebfad97ba6b4becbca9c 100644 --- a/alg.md +++ b/alg.md @@ -1,36 +1,60 @@ -# 算法设计与分析第三次作业 -#### Description -{**When you're done, you can delete the content in this README and update the file with details for others getting started with your repository**} -#### Software Architecture -Software architecture description - -#### Installation - -1. xxxx -2. xxxx -3. xxxx - -#### Instructions - -1. xxxx -2. xxxx -3. xxxx - -#### Contribution - -1. Fork the repository -2. Create Feat_xxx branch -3. Commit your code -4. Create Pull Request - - -#### Gitee Feature - -1. You can use Readme\_XXX.md to support different languages, such as Readme\_en.md, Readme\_zh.md -2. Gitee blog [blog.gitee.com](https://blog.gitee.com) -3. Explore open source project [https://gitee.com/explore](https://gitee.com/explore) -4. The most valuable open source project [GVP](https://gitee.com/gvp) -5. The manual of Gitee [https://gitee.com/help](https://gitee.com/help) -6. The most popular members [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/) +# 解题报告:不同的二叉搜索树 + +## 题目描述 + +```markdown +给你一个整数 n ,求恰由 n 个节点组成且节点值从 1 到 n 互不相同的二叉搜索树有多少种?返回满足题意的二叉搜索树的种数。 +``` + +## 思路分析 +​ 因为二叉搜索树的定义是左子树的节点值都小于根节点的值,右子树的节点值都大于根节点的值,所以对于任意的一个根节点,它的组成方式就是左子树的组成方式乘以右子树的组成方式。因为左子树和右子树的组成方式不影响对方的形态,所以问题可以使用动态规划来解决。 + +​ 设f(n)为n个节点的可构造二叉搜索树的数量。当n=0或n=1时,f(n)为1。当n>1时,根据上述分析可得出转移方程f(n)=\sum_{i=1}^{i=n} f(i-1)\times f(n-i),其中i为根节点的值,取值范围为[1, n]。最终结果为f(n)。 + +## 代码实现 + +```python +def numTrees(self, n: int) -> int: + # 定义一个数组存储二叉搜索树的数量 + dp = [0] * (n+1) + # 初始化,空树也算一种二叉搜索树 + dp[0] = 1 + dp[1] = 1 + # 遍历节点数从2到n的所有情况 + for i in range(2, n+1): + # 遍历以j为根节点的所有情况 + for j in range(1, i+1): + # 左子树的节点数 + left = j - 1 + # 右子树的节点数 + right = i - j + # 乘法原理,左子树和右子树的数量相乘 + dp[i] += dp[left] * dp[right] + return dp[n] +``` + +## 测试示例 + +​ 下面是一个测试代码的例子: + +gen.py: + +```py +import random +# 生成随机整数序列 +def data(n): + return [random.randint(1, 19) for _ in range(n)] +``` +test.py: + +```python +import gen +import alg +arr = gen.data(1) +n = arr[0] +print(n) +a = alg.numTrees(n) +print(a) +``` diff --git a/alg.py b/alg.py index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..e0251b4ca01b58bbc32dece3fd8df72aaae53504 100644 --- a/alg.py +++ b/alg.py @@ -0,0 +1,17 @@ +def numTrees(n: int): + # 定义一个数组存储二叉搜索树的数量 + dp = [0] * (n+1) + # 初始化,空树也算一种二叉搜索树 + dp[0] = 1 + dp[1] = 1 + # 遍历节点数从2到n的所有情况 + for i in range(2, n+1): + # 遍历以j为根节点的所有情况 + for j in range(1, i+1): + # 左子树的节点数 + left = j - 1 + # 右子树的节点数 + right = i - j + # 乘法原理,左子树和右子树的数量相乘 + dp[i] += dp[left] * dp[right] + return dp[n] diff --git a/gen.py b/gen.py index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..74c9aff9d50b2c3bf222e7da760b1f4055fbc9d9 100644 --- a/gen.py +++ b/gen.py @@ -0,0 +1,4 @@ +import random +# 生成随机整数序列 +def data(n): + return [random.randint(1, 19) for _ in range(n)] diff --git a/test.py b/test.py index c6e375a857503f5acaebaed0e87aa732eba389db..76bd13df9a6a78272b66da00fd01d41c31c88693 100644 --- a/test.py +++ b/test.py @@ -1,39 +1,7 @@ -# 算法设计与分析第三次作业 - -#### 介绍 -{**以下是 Gitee 平台说明,您可以替换此简介** -Gitee 是 OSCHINA 推出的基于 Git 的代码托管平台(同时支持 SVN)。专为开发者提供稳定、高效、安全的云端软件开发协作平台 -无论是个人、团队、或是企业,都能够用 Gitee 实现代码托管、项目管理、协作开发。企业项目请看 [https://gitee.com/enterprises](https://gitee.com/enterprises)} - -#### 软件架构 -软件架构说明 - - -#### 安装教程 - -1. xxxx -2. xxxx -3. xxxx - -#### 使用说明 - -1. xxxx -2. xxxx -3. xxxx - -#### 参与贡献 - -1. Fork 本仓库 -2. 新建 Feat_xxx 分支 -3. 提交代码 -4. 新建 Pull Request - - -#### 特技 - -1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md -2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com) -3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目 -4. [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目 -5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) -6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/) +import gen +import alg +arr = gen.data(1) +n = arr[0] +print(n) +a = alg.numTrees(n) +print(a) diff --git "a/\345\261\217\345\271\225\346\210\252\345\233\276 2023-04-25 203806.png" "b/\345\261\217\345\271\225\346\210\252\345\233\276 2023-04-25 203806.png" new file mode 100644 index 0000000000000000000000000000000000000000..5535647eeebd1e43c4a935443371f26309888698 Binary files /dev/null and "b/\345\261\217\345\271\225\346\210\252\345\233\276 2023-04-25 203806.png" differ