当前位置:网站首页>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] <= 104nums按 严格递增 顺序排列
其实我觉得这题难度设置为中等也不错
方法一:数组中值为根,中值左右分别为左右子树
顾名思义,要想让这棵树为高度平衡的二叉搜索树,我们只需要把数组的中值作为根即可。
因为把数组的中值作为根,即可达到左边右边的元素数量相等(或相差一个)的效果。
同时,题目给定的是一颗已经排序过的数组,因此数组中值左边的元素全部小于中值元素,右边全部大于。
因此把中值左边全部作为左子树,右边全部作为右子树即可。
这就变成了子问题:左右子树的构建。因此递归即可。
终止条件:要构建的数组为空。
- 时间复杂度 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
边栏推荐
- Sword finger offer 05 Replace spaces
- AtCoder Grand Contest 013 E - Placing Squares
- Animation scoring data analysis and visualization and it industry recruitment data analysis and visualization
- Sword finger offer 04 Search in two-dimensional array
- Cluster script of data warehouse project
- Sword finger offer 58 - ii Rotate string left
- How many checks does kubedm series-01-preflight have
- One question per day 1765 The highest point in the map
- 【Jailhouse 文章】Jailhouse Hypervisor
- Time of process
猜你喜欢

Gbase database helps the development of digital finance in the Bay Area

剑指 Offer 05. 替换空格

Sword finger offer 09 Implementing queues with two stacks

Graduation project of game mall

Dynamic planning solution ideas and summary (30000 words)
![[article de jailhouse] jailhouse hypervisor](/img/f4/4809b236067d3007fa5835bbfe5f48.png)
[article de jailhouse] jailhouse hypervisor

挂起等待锁 vs 自旋锁(两者的使用场合)

Wazuh开源主机安全解决方案的简介与使用体验

剑指 Offer 06.从头到尾打印链表

Analysis of backdoor vulnerability in remote code execution penetration test / / phpstudy of national game title of national secondary vocational network security B module
随机推荐
EOJ 2021.10 E. XOR tree
Little known skills of Task Manager
读者写者模型
Solution to the palindrome string (Luogu p5041 haoi2009)
Alu logic operation unit
API related to TCP connection
剑指 Offer 53 - I. 在排序数组中查找数字 I
AtCoder Grand Contest 013 E - Placing Squares
Zheng Qing 21 ACM is fun. (3) part of the problem solution and summary
The number of enclaves
Reflection summary of Haut OJ freshmen on Wednesday
Introduction to convolutional neural network
Haut OJ 2021 freshmen week II reflection summary
Daily question 2006 Number of pairs whose absolute value of difference is k
ssh免密登录设置及使用脚本进行ssh登录并执行指令
[practical skills] how to do a good job in technical training?
7. Processing the input of multidimensional features
剑指 Offer 09. 用两个栈实现队列
Reader writer model
Acwing 4301. Truncated sequence