当前位置:网站首页>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
边栏推荐
- ALU逻辑运算单元
- Wazuh開源主機安全解决方案的簡介與使用體驗
- Using HashMap to realize simple cache
- Personal developed penetration testing tool Satania v1.2 update
- Convolution neural network -- convolution layer
- Over fitting and regularization
- The number of enclaves
- PC寄存器
- A problem and solution of recording QT memory leakage
- 浅谈JVM(面试常考)
猜你喜欢
Smart construction site "hydropower energy consumption online monitoring system"
Support multi-mode polymorphic gbase 8C database continuous innovation and heavy upgrade
Sword finger offer 53 - ii Missing numbers from 0 to n-1
sync. Interpretation of mutex source code
全国中职网络安全B模块之国赛题远程代码执行渗透测试 //PHPstudy的后门漏洞分析
shared_ Repeated release heap object of PTR hidden danger
EOJ 2021.10 E. XOR tree
lxml. etree. XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
剑指 Offer 58 - II. 左旋转字符串
Remote upgrade afraid of cutting beard? Explain FOTA safety upgrade in detail
随机推荐
884. Uncommon words in two sentences
In this indifferent world, light crying
从Dijkstra的图灵奖演讲论科技创业者特点
Pointnet++ learning
CCPC Weihai 2021m eight hundred and ten thousand nine hundred and seventy-five
kubeadm系列-01-preflight究竟有多少check
Solution to game 10 of the personal field
剑指 Offer 53 - II. 0~n-1中缺失的数字
After setting up the database and website When you open the app for testing, it shows that the server is being maintained
【Jailhouse 文章】Jailhouse Hypervisor
【云原生】微服务之Feign自定义配置的记录
Remote upgrade afraid of cutting beard? Explain FOTA safety upgrade in detail
YOLOv5-Shufflenetv2
How many checks does kubedm series-01-preflight have
二十六、文件系统API(设备在应用间的共享;目录和文件API)
Time complexity and space complexity
Animation scoring data analysis and visualization and it industry recruitment data analysis and visualization
Daily question 2006 Number of pairs whose absolute value of difference is k
Haut OJ 1401: praise energy
CF1637E Best Pair