forked from zapellass123/OpenEmailGenerator
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArray to BST.cpp
More file actions
43 lines (35 loc) · 827 Bytes
/
Array to BST.cpp
File metadata and controls
43 lines (35 loc) · 827 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<bits/stdc++.h>
using namespace std;
class Solution {
public:
vector<int> sortedArrayToBST(vector<int>& nums) {
vector<int> v;
helper(nums, 0, nums.size()-1, v);
return v;
}
void helper(vector<int>&nums, int low, int high, vector<int>&v){
if(low <= high){
int mid = low + (high-low)/2;
v.push_back(nums[mid]);
helper(nums, low, mid-1, v);
helper(nums, mid+1, high, v);
}
return;
}
};
int main(){
int tc;
cin >> tc;
while(tc--){
int n;
cin >> n;
vector<int>nums(n);
for(int i = 0; i < n; i++)cin >> nums[i];
Solution obj;
vector<int>ans = obj.sortedArrayToBST(nums);
for(auto i: ans)
cout << i <<" ";
cout << "\n";
}
return 0;
}