当前位置:网站首页>LeetCode 0108. Convert an ordered array into a binary search tree - the median of the array is the root, and the left and right of the median are the left and right subtrees respectively
LeetCode 0108. Convert an ordered array into a binary search tree - the median of the array is the root, and the left and right of the median are the left and right subtrees respectively
2022-07-05 06:10:00 【Tisfy】
【LetMeFly】108. Convert an ordered array to a binary search tree - The value in the array is the root , The left and right median values are respectively left and right subtrees
Force button topic link :https://leetcode.cn/problems/convert-sorted-array-to-binary-search-tree/
Give you an array of integers nums
, Where elements have been Ascending array , Please turn it into a Highly balanced Binary search tree .
Highly balanced A binary tree is a tree that satisfies 「 The absolute value of the height difference between the left and right subtrees of each node does not exceed 1 」 Two fork tree .
Example 1:
Input :nums = [-10,-3,0,5,9] Output :[0,-3,9,-10,null,5] explain :[0,-10,5,null,-3,null,9] Will also be considered the right answer :
Example 2:
Input :nums = [1,3] Output :[3,1] explain :[1,null,3] and [3,1] They're all highly balanced binary search trees .
Tips :
1 <= nums.length <= 104
-104 <= nums[i] <= 104
nums
Press Strictly increasing Sequential arrangement
In fact, I think it's good to set the difficulty of this question to medium
Method 1 : The value in the array is the root , The left and right median values are respectively left and right subtrees
seeing the name of a thing one thinks of its function , To make this tree a highly balanced binary search tree , We just need to take the median of the array as the root .
Because take the median of the array as the root , The number of elements on the left and right is equal ( Or one difference ) The effect of .
meanwhile , What the title gives is an array that has been sorted , Therefore, the elements on the left of the value in the array are all smaller than the median elements , All on the right are larger than .
Therefore, the left side of the median is regarded as the left subtree , All the right trees can be regarded as right subtrees .
This becomes a sub problem : Construction of left and right subtrees . So recursion is enough .
Termination conditions : The array to be built is empty .
- Time complexity O ( n ) O(n) O(n), among n n n Is the number of elements in the array .
- Spatial complexity O ( log n ) O(\log n) O(logn), Depending on the depth of the recursive stack .
AC Code
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());
}
};
Synchronous posting on CSDN, Originality is not easy. , Reprint please attach Link to the original text Oh ~
Tisfy:https://letmefly.blog.csdn.net/article/details/125610878
边栏推荐
- Arduino 控制的 RGB LED 无限镜
- Daily question 1984 Minimum difference in student scores
- 【Rust 笔记】17-并发(下)
- Typical use cases for knapsacks, queues, and stacks
- 中职网络安全技能竞赛——广西区赛中间件渗透测试教程文章
- Spark中groupByKey() 和 reduceByKey() 和combineByKey()
- The connection and solution between the shortest Hamilton path and the traveling salesman problem
- [rust notes] 14 set (Part 1)
- QT判断界面当前点击的按钮和当前鼠标坐标
- One question per day 1765 The highest point in the map
猜你喜欢
[jailhouse article] jailhouse hypervisor
Open source storage is so popular, why do we insist on self-development?
Wazuh开源主机安全解决方案的简介与使用体验
Appium foundation - use the first demo of appium
Navicat連接Oracle數據庫報錯ORA-28547或ORA-03135
SPI 详解
Groupbykey() and reducebykey() and combinebykey() in spark
快速使用Amazon MemoryDB并构建你专属的Redis内存数据库
Implement an iterative stack
[practical skills] technical management of managers with non-technical background
随机推荐
LaMDA 不可能觉醒吗?
Wazuh开源主机安全解决方案的简介与使用体验
“磐云杯”中职网络安全技能大赛A模块新题
Navicat連接Oracle數據庫報錯ORA-28547或ORA-03135
R language [import and export of dataset]
F - Two Exam(AtCoder Beginner Contest 238)
【Rust 笔记】16-输入与输出(上)
Solution to game 10 of the personal field
Liunx starts redis
智慧工地“水电能耗在线监测系统”
Daily question 1688 Number of matches in the competition
leetcode-3:无重复字符的最长子串
R语言【数据集的导入导出】
Collection: programming related websites and books
Real time clock (RTC)
Brief introduction to tcp/ip protocol stack
Multi screen computer screenshots will cut off multiple screens, not only the current screen
数据可视化图表总结(一)
LeetCode 0107.二叉树的层序遍历II - 另一种方法
Bit mask of bit operation