[LeetCode]题解(python):108-Convert Sorted Array to Binary Search Tree
题目来源:
https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/
题意分析:
给出一个排好序的数组,根据这个数据形成一个高度平衡的搜索二叉树。
题目思路:
将中位数为根节点,中位数左边为左子树,右边为右子树。
代码(python):
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = Noneclass Solution(object):def sortedArrayToBST(self, nums):""":type nums: List[int]:rtype: TreeNode"""size = len(nums)if size == 0:return Noneif size == 1:return TreeNode(nums[0])size //= 2root = TreeNode(nums[size])root.left = self.sortedArrayToBST(nums[:size])root.right = self.sortedArrayToBST(nums[size + 1:])return root
View Code
转载于:https://www.cnblogs.com/chruny/p/5258311.html
[LeetCode]题解(python):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 python3 简单题108. Convert Sorted Array to Binary Search Tree
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百零八题 (1)题目 英文: Given an array where elem ...
- 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 ...
- 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 ...
- 【leetcode刷题笔记】Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 题解 ...
最新文章
- Python3中的json对象与python对象转化
- 中兴核心网服务器笔记本电脑,中兴通讯5G Common Core
- python读取时间_python 获取时间
- spring cloud入门_Spring Boot 2.x基础教程:快速入门
- CodeForces - 1607D Blue-Red Permutation(贪心)
- CodeForces - 1058D D. Vasya and Triangle
- PHP array_count_values() 函数用于统计数组中所有值出现的次数。
- “阿里云核心竞争力”峰会首日中奖小伙伴名单公布!机械键盘等豪礼下午继续放出!...
- vnc远程控制软件怎么用,在Windows中vnc远程控制软件怎么用
- 腾讯云域名证书哪里下载_备案域名证书获取方法
- C语言逻辑操作符的巧妙用法:an anagram of a palindrome
- 【关闭Antimalware Service Executable(windows defender)】
- 收藏的书录,值得花时间去读的书
- oracle 修改 dbid,Oracle 在使用NID更改DBID时ORA-01122 ORA-01110 ORA-01206
- mongoDB 入门必看
- WOW游戏中装备和材料的提示框
- Windows10+Ubuntu18.04双系统安装成功心得(理清网上杂乱无章的教程)
- 研发绩效考核按月还是按季度进行?
- PROTAC与抗体偶联药物的结合
- 新手初学者怎样才能学好Java?
热门文章
- java bufferedwriter 写入tab_一个Java程序员的成长历程(014,015)天
- jvm调优工具_JVM性能调优监控工具jps、jstack、jmap、jhat、hprof使用详解
- python小工具封装_python接口自动化(二)——封装需要用到的工具类
- Docker初学3:Docker的常用命令
- python缩进在程序中长度统一且强制使用_Python习题纠错1
- 微信小程序动态设置 tabBar
- dva + antd + mockjs 实现用户管理
- Journey源码分析三:模板编译
- Mozilla公布WebVR API标准草案
- jquery兼容IE和火狐下focus()事件