Created
March 8, 2019 01:09
-
-
Save qiaoxu123/dc59587213b815fd21b615d5f18adaeb to your computer and use it in GitHub Desktop.
> 使用recursive方法进行二分,利用BST的特性
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Runtime: 24 ms, faster than 89.44% | |
//Memory Usage: 21.1 MB, less than 56.68% | |
class Solution { | |
public: | |
TreeNode* sortedArrayToBST(vector<int>& nums) { | |
return sortedArrayToBST(nums,0,nums.size()); | |
} | |
TreeNode* sortedArrayToBST(vector<int>& nums,int start,int end){ | |
if(end <= start) return NULL; | |
int midIdx = (end + start) / 2; | |
TreeNode* root = new TreeNode(nums[midIdx]); | |
root->left = sortedArrayToBST(nums,start,midIdx); | |
root->right = sortedArrayToBST(nums,midIdx + 1,end); | |
return root; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment