From 36c63fd4ba56533cf4d164321c516d2b536eca4b Mon Sep 17 00:00:00 2001 From: look Date: Tue, 11 Apr 2023 21:20:22 +0800 Subject: [PATCH] =?UTF-8?q?look=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- alg.py | 28 ++++++ gen.py | 1 + merge.cpp | 90 ------------------- test.py | 57 ++++-------- ...47\344\272\214\345\217\211\346\240\221.md" | 85 ++++++++++++++++++ 5 files changed, 133 insertions(+), 128 deletions(-) delete mode 100644 merge.cpp create mode 100644 "\350\247\243\351\242\230\346\212\245\345\221\212\357\274\232\346\236\204\345\273\272\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" diff --git a/alg.py b/alg.py index e69de29..35282b9 100644 --- a/alg.py +++ b/alg.py @@ -0,0 +1,28 @@ +# 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 e69de29..2c00ed3 100644 --- a/gen.py +++ b/gen.py @@ -0,0 +1 @@ +nums = [3,2,1,6,0,5] diff --git a/merge.cpp b/merge.cpp deleted file mode 100644 index d2556a0..0000000 --- a/merge.cpp +++ /dev/null @@ -1,90 +0,0 @@ -#include -#include -using namespace std; - - -void merge(int* a, int low, int mid, int hight) //ϲ -{ - int* b = new int[hight - low + 1]; // new һ - int i = low, j = mid + 1, k = 0; // kΪ b С - while (i <= mid && j <= hight) - { - if (a[i] <= a[j]) - { - b[k++] = a[i++]; //С b - } - else - { - b[k++] = a[j++]; - } - } - while (i <= mid) // j нʣ i в b - { - b[k++] = a[i++]; - } - while (j <= hight)// i нʣ j в b - { - b[k++] = a[j++]; - } - k = 0; //СΪ 0 ʼ - for (int i = low; i <= hight; i++) // b ֵݸ a - { - a[i] = b[k++]; - } - delete[]b; // 󣬽Ŀռͷţ٣ -} - - - -void mergesort(int* a, int low, int hight) //鲢 -{ - if (low < hight) - { - int mid = (low + hight) / 2; - mergesort(a, low, mid); // a[low,mid] - mergesort(a, mid + 1, hight); // a[mid+1,hight] - merge(a, low, mid, hight); //кϲ - } -} - - - - -int main() -{ - int num[5] = { 0 }; - while (num[4] == 0) - { - srand((int)time(0)); // 0NULLҲ - for (int i = 0; i < 5; i++) - { - int a = (rand() % 100) + 1; - num[i] = a; - } - } - cout << "飺" << endl; - for (int i = 0; i < 5; i++) - cout << num[i] << ' '; - cout << endl; - - mergesort(num, 0, 4); - cout << "鲢Ľ" << endl; - for (int i = 0; i < 5; i++) - cout << num[i] << ' '; - cout << endl; - - int k; - cout << "k:"; - cin >> k; - if (k > 4) { - cout << "С"; - return 0; - } - cout << "kΪ"; - cout << num[k - 1]; - - - return 0; - - -} \ No newline at end of file diff --git a/test.py b/test.py index 6e43fcf..2b17793 100644 --- a/test.py +++ b/test.py @@ -1,38 +1,19 @@ -# 算法设计第二次作业 - -#### 介绍 -一起做作业5555555 - - -#### 软件架构 -软件架构说明 - - -#### 安装教程 - -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 + +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)) diff --git "a/\350\247\243\351\242\230\346\212\245\345\221\212\357\274\232\346\236\204\345\273\272\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" "b/\350\247\243\351\242\230\346\212\245\345\221\212\357\274\232\346\236\204\345\273\272\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" new file mode 100644 index 0000000..5f22036 --- /dev/null +++ "b/\350\247\243\351\242\230\346\212\245\345\221\212\357\274\232\346\236\204\345\273\272\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" @@ -0,0 +1,85 @@ +# 解题报告:构建最大二叉树 + +## 题目描述 + +​ 给定一个不重复的整数数组 `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)$。 + -- Gitee