-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8.20
More file actions
19 lines (17 loc) · 645 Bytes
/
8.20
File metadata and controls
19 lines (17 loc) · 645 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
분할정복
https://leetcode.com/problem-list/divide-and-conquer/
https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/description/?envType=problem-list-v2&envId=divide-and-conque
class Solution {
public:
TreeNode* sortedArrayToBST(vector<int>& nums) {
return build(nums, 0, nums.size());
}
TreeNode* build(vector<int>& nums, int start, int end) {
if (start == end) return nullptr;
int mid = (start + end) / 2;
TreeNode* root = new TreeNode(nums[mid]);
root->left = build(nums, start, mid);
root->right = build(nums, mid + 1, end);
return root;
}
};