leetcode python3 简单题108. Convert Sorted Array to Binary Search Tree
1.编辑器
我使用的是win10+vscode+leetcode+python3
环境配置参见我的博客:
链接
2.第一百零八题
(1)题目
英文:
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
中文:
将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索树。
本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/convert-sorted-array-to-binary-search-tree
(2)解法
使用递归,二分,关键点就是,数组是有序的,所以用二分可以满足节点大于左子树任意节点,小于右子树任意节点。
(耗时:56ms,内存:15.7M)
class Solution:def sortedArrayToBST(self, nums: List[int]) -> TreeNode:if nums:m = len(nums) // 2r = TreeNode(nums[m])r.left, r.right = map(self.sortedArrayToBST, [nums[:m], nums[m+1:]])return r
注意:
1.使用map要使用iterable变量哦,比如list[[1], [2,3]]这样的。
2.如果向sortedArrayToBST函数输入一个None或者空的list[],会return None。
3.若nums=[1],则nums[:0]=[]空,nums[1:]=[]空,而不是None;并且nums[:1]=[1]而不是1。
leetcode python3 简单题108. Convert Sorted Array to Binary Search Tree相关推荐
- 【数组递归构造二叉树】LeetCode 108. Convert Sorted Array to Binary Search Tree
LeetCode 108. Convert Sorted Array to Binary Search Tree Solution1:我的答案 构造二叉树利用递归 /*** Definition fo ...
- LeetCode: 108. Convert Sorted Array to Binary Search Tree
题目 Given an array where elements are sorted in ascending order, convert it to a height balanced BST. ...
- LeetCode 108. Convert Sorted Array to Binary Search Tree
题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST ...
- leetcode 108. Convert Sorted Array to Binary Search Tree | 108. 将有序数组转换为二叉搜索树(Java)
题目 https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ 题解 经典二分,不解释了,直接看代码 /*** ...
- 108. Convert Sorted Array to Binary Search Tree
一.题目 1.审题 2.分析 给出一个有序的不重复的整形数组,组成一个二路平衡二叉树(左右子树高度差不超过1). 二.解答 1.思路: 方法一. 采用递归的方式.(像二分查找) ①.为了保持平衡,查找 ...
- Convert Sorted Array to Binary Search Tree - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Convert Sorted Array to Binary Search Tree - LeetCode 注意点 不要访问空结点 题目要求的是平衡二叉搜 ...
- LeetCode: Convert Sorted Array to Binary Search Tree 解题报告
Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...
- leetcode python3 简单题88. Merge Sorted Array
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第八十八题 (1)题目 英文: Given two sorted integer a ...
- Convert Sorted Array to Binary Search Tree With Minimal Height
Given a sorted (increasing order) array, Convert it to create a binary tree with minimal height. Exa ...
最新文章
- fanuc机器人提示暂停_川崎机器人故障排除参考方法
- CentOS 6.5 部署WordPress
- django 集成个推_持续集成CircleCI vs Travis CI vs Jenkins
- WEB接口测试之Jmeter接口测试自动化 (三)(数据驱动测试) 接口测试与数据驱动...
- spring中使用scala的maven打包配置
- 数据库选型入门必读:如何在眼花缭乱的产品中挑出最适合业务的?
- border属性 php,如何通过CSS的border属性为图片设置边框效果
- python scrapy爬虫遇见301_python Scrapy框架 报301
- 代码整洁之道读书笔记----第三章---函数--第一节-专注且短小
- 【毕业设计】深度学习指纹识别算法研究与实现 - python opencv
- 跳过Android6.0+权限的方法
- 软件开发人才级别的划分
- 复盘618:数字狂欢背后透露了哪些隐藏信号?
- 【管理篇 / 诊断】❀ 01. 了解防火墙的性能参数 ❀ FortiGate 防火墙
- (dfppy)2Ir(NHC)的蓝光/蓝绿光铱配合物|苯基喹啉酯的中性铱配合物-齐岳生物
- 对于刚毕业的学弟学妹对即将要就业产生迷茫,下面我来为大家揭晓该如何选择!
- 使用openssl制作https的证书
- 音频降噪算法 java_基于RNN的音频降噪算法
- 跟阿里巴巴学开会,我们拉着5位CEO聊如何“捅刀子”
- 开发环境配置 -- 集成