-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLC_108_SortedArrayToBST.cpp
83 lines (74 loc) · 1.94 KB
/
LC_108_SortedArrayToBST.cpp
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/
108. Convert Sorted Array to Binary Search Tree
*/
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
// LEETCODE require to construct the tree.
class Solution {
public:
TreeNode* sortedArrayToBST(vector<int>& nums) {
int n = nums.size();
if(n==0) return nullptr;
TreeNode* root = insertIntoBST(nums, 0, n-1);
return root;
}// end
TreeNode* insertIntoBST(vector<int>& nums, int l, int r)
{
if(l>r) return nullptr;
int mid = l+(r-l)/2;
TreeNode* root = new TreeNode(nums[mid]);
root->left = insertIntoBST(nums, l, mid-1);
root->right = insertIntoBST(nums, mid+1, r);
return root;
}
};
// GFG, Doesn't require to construct the tree.
// { Driver Code Starts
#include<bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution {
public:
vector<int> ans;
vector<int> sortedArrayToBST(vector<int>& nums) {
// Code here
int n = nums.size();
preorder(nums, 0, n-1);
return ans;
}
void preorder(vector<int>& nums, int s, int e)
{
if(s>e) return;
int mid = s + (e-s)/2;
ans.push_back(nums[mid]);
preorder(nums, s, mid-1);
preorder(nums, mid+1, e);
}
};
// { Driver Code Starts.
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;
} // } Driver Code Ends