diff --git a/alg.md b/alg.md index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..8a4ef77a526d0776d42aca4d058e678daa13d96a 100644 --- a/alg.md +++ b/alg.md @@ -0,0 +1,84 @@ +# 解题报告:构建最大二叉树 + +## 题目描述 + +​ 给定一个不重复的整数数组 `nums`,构建一个最大二叉树,其中根节点的值为 `nums` 中的最大值,左子树是构建在最大值左边的子数组的最大二叉树,右子树是构建在最大值右边的子数组的最大二叉树。 + +## 思路分析 + +​ 对于一个不重复的整数数组 `nums`,我们可以通过以下步骤构建一个最大二叉树: + +1. 找到数组中的最大值及其索引。 +2. 创建根节点,其值为最大值。 +3. 递归地在最大值左边的子数组前缀上构建左子树。 +4. 递归地在最大值右边的子数组后缀上构建右子树。 +5. 返回构建好的最大二叉树的根节点。 + +## 代码实现 + +​ 下面是python代码实现,其中`TreeNode`类定义了二叉树节点的结构,`constructMaximumbinaryTree`函数接收一个整数数组 `nums`,返回该数组构建的最大二叉树的根节点。在函数内部,首先判断如果 `nums` 数组为空,则直接返回 `None`。否则,找到数组中的最大值及其索引,创建根节点,其值为最大值。然后,递归地在最大值左边的子数组前缀上构建左子树,递归地在最大值右边的子数组后缀上构建右子树。最后,返回构建好的最大二叉树的根节点。 + +```python +# Definition for a binary tree node. +class TreeNode: + def __init__(self, val=0, left=None, right=None): + self.val = val + self.left = left + self.right = right + +def constructMaximumBinaryTree(nums): + # 如果数组为空,则返回None + if not nums: + return None + + max_num = max(nums) + + # 找到数组中的最大值及其索引 + idx = nums.index(max_num) + + # 创建根节点,其值为最大值 + root = TreeNode(max_num) + + # 递归地在最大值左边的子数组前缀上构建左子树 + root.left = constructMaximumBinaryTree(nums[:idx]) + + # 递归地在最大值右边的子数组后缀上构建右子树 + root.right = constructMaximumBinaryTree(nums[idx+1:]) + + # 返回构建好的最大二叉树的根节点 + return root + +``` + +## 测试示例 + +​ 下面是一个测试代码的例子: + +```python +import alg +nums = [3, 2, 1, 6, 0, 5] +# 构建最大二叉树 +root = alg.constructMaximumBinaryTree(nums) +# 前序遍历二叉树 +def preorderTraversal(root): + res = [] + def helper(root): + # 如果树为空,则返回None + if not root: + return None + res.append(root.val) + helper(root.left) + helper(root.right) + helper(root) + return res +# 打印遍历结果 +print(preorderTraversal(root)) +``` + +​ 这个测试代码将一个整数数组传递给 `constructMaximumBinaryTree` 函数,得到该数组构建的最大二叉树的根节点。然后,使用 `preorderTraversal` 函数进行前序遍历,并打印遍历结果。运行该测试代码将输出 `[6,3,2,1,5,0]`,即最大二叉树的前序遍历结果。 + +## 时间复杂度分析 + +​ 对于一个长度为 `n` 的不重复的整数数组 `nums`,构建最大二叉树的时间复杂度为 $O(n\log n)$。 + +​ 在构建最大二叉树的过程中,每个节点最多会被访问两次,因此时间复杂度为 $O(n)$。而在最坏情况下,数组中的每个数都会成为树的根节点,因此二叉树的深度为 $O(n)$,所以总的时间复杂度为 $O(n\log n)$。 diff --git a/alg.py b/alg.py index ab507dc2f914d4b0e8ab7acccc195509b7f5e598..35282b9f56200c1ec6a2f6538e6d3ec44d9958d3 100644 --- a/alg.py +++ b/alg.py @@ -1,39 +1,28 @@ -# 算法设计与分析第二次作业 - -#### 介绍 -{**以下是 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/) +# Definition for a binary tree node. +class TreeNode: + def __init__(self, val=0, left=None, right=None): + self.val = val + self.left = left + self.right = right + +def constructMaximumBinaryTree(nums): + # 如果数组为空,则返回None + if not nums: + return None + + max_num = max(nums) + + # 找到数组中的最大值及其索引 + idx = nums.index(max_num) + + # 创建根节点,其值为最大值 + root = TreeNode(max_num) + + # 递归地在最大值左边的子数组前缀上构建左子树 + root.left = constructMaximumBinaryTree(nums[:idx]) + + # 递归地在最大值右边的子数组后缀上构建右子树 + root.right = constructMaximumBinaryTree(nums[idx+1:]) + + # 返回构建好的最大二叉树的根节点 + return root diff --git a/gen.py b/gen.py index 4348e82fbdf9d2089678f8a6dbaf1f0f8c32fc59..1c16f48d7f6b44ce2ec650a8a22ff7dadd368a72 100644 --- a/gen.py +++ b/gen.py @@ -1,36 +1,4 @@ -# 算法设计与分析第二次作业 +nums = [3,2,1,6,0,5] -#### 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/) diff --git a/test.py b/test.py index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..3af305458bceaa91bf31e34600e040086adbadbe 100644 --- a/test.py +++ b/test.py @@ -0,0 +1,19 @@ +import gen +import alg + +n = gen.nums +root = alg.constructMaximumBinaryTree(n) +# 前序遍历二叉树 +def preorderTraversal(root): + res = [] + def helper(root): + # 如果树为空,则返回None + if not root: + return None + res.append(root.val) + helper(root.left) + helper(root.right) + helper(root) + return res +# 输出遍历结果 +print(preorderTraversal(root))