当前位置:网站首页>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
边栏推荐
猜你喜欢
Open source storage is so popular, why do we insist on self-development?
Dynamic planning solution ideas and summary (30000 words)
Solution to game 10 of the personal field
1.13 - RISC/CISC
可变电阻器概述——结构、工作和不同应用
Scope of inline symbol
LVS简介【暂未完成(半成品)】
[practical skills] how to do a good job in technical training?
SQLMAP使用教程(一)
Full Permutation Code (recursive writing)
随机推荐
Convolution neural network -- convolution layer
Spark中groupByKey() 和 reduceByKey() 和combineByKey()
7. Processing the input of multidimensional features
[rust notes] 15 string and text (Part 1)
Solution to game 10 of the personal field
智慧工地“水电能耗在线监测系统”
[rust notes] 16 input and output (Part 2)
Daily question 2006 Number of pairs whose absolute value of difference is k
leetcode-556:下一个更大元素 III
[practical skills] technical management of managers with non-technical background
Doing SQL performance optimization is really eye-catching
Dynamic planning solution ideas and summary (30000 words)
LeetCode 0108.将有序数组转换为二叉搜索树 - 数组中值为根,中值左右分别为左右子树
The connection and solution between the shortest Hamilton path and the traveling salesman problem
Introduction to convolutional neural network
Control unit
One question per day 1765 The highest point in the map
CPU内核和逻辑处理器的区别
Binary search template
“磐云杯”中职网络安全技能大赛A模块新题