当前位置:网站首页>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
边栏推荐
- 注解与反射
- 中职网络安全技能竞赛——广西区赛中间件渗透测试教程文章
- Kubedm series-00-overview
- Pointnet++ learning
- Codeforces round 712 (Div. 2) d. 3-coloring (construction)
- On the characteristics of technology entrepreneurs from Dijkstra's Turing Award speech
- Haut OJ 2021 freshmen week II reflection summary
- 卷积神经网络——卷积层
- 【Jailhouse 文章】Jailhouse Hypervisor
- The number of enclaves
猜你喜欢
从Dijkstra的图灵奖演讲论科技创业者特点
Analysis of backdoor vulnerability in remote code execution penetration test / / phpstudy of national game title of national secondary vocational network security B module
[cloud native] record of feign custom configuration of microservices
How to adjust bugs in general projects ----- take you through the whole process by hand
剑指 Offer 05. 替换空格
CCPC Weihai 2021m eight hundred and ten thousand nine hundred and seventy-five
CF1634E Fair Share
剑指 Offer 58 - II. 左旋转字符串
【实战技能】如何做好技术培训?
读者写者模型
随机推荐
Solution to game 10 of the personal field
动漫评分数据分析与可视化 与 IT行业招聘数据分析与可视化
剑指 Offer 05. 替换空格
Educational Codeforces Round 116 (Rated for Div. 2) E. Arena
Support multi-mode polymorphic gbase 8C database continuous innovation and heavy upgrade
Over fitting and regularization
Codeforces Round #732 (Div. 2) D. AquaMoon and Chess
Little known skills of Task Manager
Fried chicken nuggets and fifa22
Brief introduction to tcp/ip protocol stack
One question per day 2047 Number of valid words in the sentence
Detailed explanation of expression (csp-j 2021 expr) topic
YOLOv5-Shufflenetv2
Light a light with stm32
[practical skills] technical management of managers with non-technical background
Sword finger offer 58 - ii Rotate string left
Sword finger offer 35 Replication of complex linked list
Remote upgrade afraid of cutting beard? Explain FOTA safety upgrade in detail
Add level control and logger level control of Solon logging plug-in
Solution to the palindrome string (Luogu p5041 haoi2009)