当前位置:网站首页>LeetCode 0108.将有序数组转换为二叉搜索树 - 数组中值为根,中值左右分别为左右子树
LeetCode 0108.将有序数组转换为二叉搜索树 - 数组中值为根,中值左右分别为左右子树
2022-07-05 05:45:00 【Tisfy】
【LetMeFly】108.将有序数组转换为二叉搜索树 - 数组中值为根,中值左右分别为左右子树
力扣题目链接:https://leetcode.cn/problems/convert-sorted-array-to-binary-search-tree/
给你一个整数数组 nums
,其中元素已经按 升序 排列,请你将其转换为一棵 高度平衡 二叉搜索树。
高度平衡 二叉树是一棵满足「每个节点的左右两个子树的高度差的绝对值不超过 1 」的二叉树。
示例 1:

输入:nums = [-10,-3,0,5,9] 输出:[0,-3,9,-10,null,5] 解释:[0,-10,5,null,-3,null,9] 也将被视为正确答案:![]()
示例 2:

输入:nums = [1,3] 输出:[3,1] 解释:[1,null,3] 和 [3,1] 都是高度平衡二叉搜索树。
提示:
1 <= nums.length <= 104
-104 <= nums[i] <= 104
nums
按 严格递增 顺序排列
其实我觉得这题难度设置为中等也不错
方法一:数组中值为根,中值左右分别为左右子树
顾名思义,要想让这棵树为高度平衡的二叉搜索树,我们只需要把数组的中值作为根即可。
因为把数组的中值作为根,即可达到左边右边的元素数量相等(或相差一个)的效果。
同时,题目给定的是一颗已经排序过的数组,因此数组中值左边的元素全部小于中值元素,右边全部大于。
因此把中值左边全部作为左子树,右边全部作为右子树即可。
这就变成了子问题:左右子树的构建。因此递归即可。
终止条件:要构建的数组为空。
- 时间复杂度 O ( n ) O(n) O(n),其中 n n n是数组中元素的个数。
- 空间复杂度 O ( log n ) O(\log n) O(logn),取决于递归栈的深度。
AC代码
C++
class Solution {
private:
TreeNode* build(vector<int>& nums, int l, int r) {
if (l >= r)
return nullptr;
int mid = (l + r) >> 1;
TreeNode* root = new TreeNode(nums[mid]);
root->left = build(nums, l, mid);
root->right = build(nums, mid + 1, r);
return root;
}
public:
TreeNode* sortedArrayToBST(vector<int>& nums) {
return build(nums, 0, nums.size());
}
};
同步发文于CSDN,原创不易,转载请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/125610878
边栏推荐
- F - Two Exam(AtCoder Beginner Contest 238)
- Sword finger offer 04 Search in two-dimensional array
- Dynamic planning solution ideas and summary (30000 words)
- Sword finger offer 05 Replace spaces
- 剑指 Offer 05. 替换空格
- 游戏商城毕业设计
- CF1637E Best Pair
- Software test -- 0 sequence
- Full Permutation Code (recursive writing)
- Codeforces Round #732 (Div. 2) D. AquaMoon and Chess
猜你喜欢
2017 USP Try-outs C. Coprimes
个人开发的渗透测试工具Satania v1.2更新
shared_ Repeated release heap object of PTR hidden danger
【实战技能】非技术背景经理的技术管理
Analysis of backdoor vulnerability in remote code execution penetration test / / phpstudy of national game title of national secondary vocational network security B module
Sword finger offer 09 Implementing queues with two stacks
【Jailhouse 文章】Look Mum, no VM Exits
YOLOv5-Shufflenetv2
SAP-修改系统表数据的方法
全排列的代码 (递归写法)
随机推荐
Reader writer model
Using HashMap to realize simple cache
Daily question 1688 Number of matches in the competition
PC寄存器
Graduation project of game mall
Add level control and logger level control of Solon logging plug-in
“磐云杯”中职网络安全技能大赛A模块新题
剑指 Offer 05. 替换空格
Light a light with stm32
[jailhouse article] performance measurements for hypervisors on embedded ARM processors
Dichotomy, discretization, etc
Some common problems in the assessment of network engineers: WLAN, BGP, switch
Introduction et expérience de wazuh open source host Security Solution
Annotation and reflection
Full Permutation Code (recursive writing)
Chapter 6 data flow modeling - after class exercises
Daily question 1342 Number of operations to change the number to 0
In this indifferent world, light crying
Fried chicken nuggets and fifa22
智慧工地“水电能耗在线监测系统”