LeetCode解題報告 108. Convert Sorted Array to Binary Search Tree [medium]
題目描述
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
解題思路
題目要求對一個已經排好序的陣列來構造一顆平衡二叉樹。
平衡二叉樹指的是對於樹上任意一個節點,其左右子樹的高度差不超過1。
若兩顆子樹節點數量相同,且使用同樣的方法來進行構造。那麼它們的形態應該是相同的,層數也相同。舉個例子:
若兩顆子樹節點數量相差1,則構造出的子樹層數至多相差1。舉個例子:
只需要保證每一次遞迴處理構造的左右子樹節點數量最多不超過1,就可以保證生成的二叉樹滿足高度平衡的性質。因此,每一次以中位數進行分割,將陣列分割為左右兩個部分,對左右兩個子樹執行相同的操作,不斷從中點劃分,來遞迴構造二叉樹。
複雜度分析
時間複雜度為O(n),空間複雜度為O(n)。
程式碼如下:
class Solution {
public:
TreeNode* sortedArrayToBST(vector<int>& nums) {
if (nums.size()==0) {
return NULL;
}
int medium=nums.size()/2;
vector<int>leftlist(nums.begin(),nums.begin()+medium);
vector<int>righlist(nums.begin()+medium+1,nums.end());
TreeNode* result=new TreeNode(nums[medium]);
result->left=sortedArrayToBST(leftlist);
result->right=sortedArrayToBST(righlist);
return result;
}
};
參考:
https://www.tianmaying.com/tutorial/LC108
相關文章
- Leetcode Convert Sorted Array to Binary Search TreeLeetCode
- [leetcode]convert-sorted-array-to-binary-search-treeLeetCode
- Convert Sorted Array to Binary Search Tree leetcode javaLeetCodeJava
- 【LeetCode】Convert Sorted List to Binary Search TreeLeetCode
- Leetcode Convert Sorted List to Binary Search TreeLeetCode
- Convert Sorted List to Binary Search Tree leetcode javaLeetCodeJava
- Java for LeetCode 109 Convert Sorted List to Binary Search TreeJavaLeetCode
- [LeetCode] 109. Convert Sorted List to Binary Search TreeLeetCode
- 【Leetcode】109. Convert Sorted List to Binary Search TreeLeetCode
- Leetcode Search in Rotated Sorted ArrayLeetCode
- Leetcode 33 Search in Rotated Sorted ArrayLeetCode
- Leetcode Search in Rotated Sorted Array IILeetCode
- Leetcode-Search in Rotated Sorted ArrayLeetCode
- Search in Rotated Sorted Array leetcode javaLeetCodeJava
- Leetcode-Convert Sorted Array to BSTLeetCode
- leetcode33_Search in Rotated Sorted ArrayLeetCode
- LeetCode-Search in Rotated Sorted Array IILeetCode
- Search in Rotated Sorted Array II leetcode javaLeetCodeJava
- Leetcode Validate Binary Search TreeLeetCode
- leetcode 之 Recover Binary Search TreeLeetCode
- Recover Binary Search Tree leetcode javaLeetCodeJava
- LeetCode-Closest Binary Search Tree ValueLeetCode
- Validate Binary Search Tree leetcode javaLeetCodeJava
- LeetCode解題報告 279. Perfect Squares [medium]LeetCode
- LeetCode 98. Validate Binary Search TreeLeetCode
- LeetCode-Closest Binary Search Tree Value IILeetCode
- LeetCode解題報告 102. Binary Tree Level Order Traversal [easy]LeetCode
- [LeetCode] 702. Search in a Sorted Array of Unknown SizeLeetCode
- [LeetCode] 501. Find Mode in Binary Search TreeLeetCode
- LeetCode 501. Find Mode in Binary Search TreeLeetCode
- LeetCode解題報告 120. Triangle [medium]LeetCode
- 173. Binary Search Tree Iterator
- LintCode-Search Range in Binary Search Tree
- LeetCode解題報告 241. Different Ways to Add Parentheses [medium]LeetCode
- LeetCode C++ 33. Search in Rotated Sorted Array【二分】中等LeetCodeC++
- [LeetCode] Search in Rotated Sorted Array 在旋轉有序陣列中搜尋LeetCode陣列
- LeetCode解題報告 452. Minimum Number of Arrows to Burst Balloons [medium]LeetCode
- LeetCode 272 Closest Binary Tree Traversal II 解題思路LeetCode