-
Notifications
You must be signed in to change notification settings - Fork 213
Expand file tree
/
Copy pathversion.cpp
More file actions
41 lines (35 loc) · 783 Bytes
/
version.cpp
File metadata and controls
41 lines (35 loc) · 783 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
#include<iostream>
#include<string.h>
#include<queue>
#include<cmath>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
TreeNode* helper(vector<int> A, int a, int b)
{
if(b >= a)
{
int mid = (b - a)/2;
TreeNode* root = new TreeNode(A[mid]);
root->left = helper(A, a, mid - 1);
root->right = helper(A, mid + 1, b);
}
else
return NULL;
}
TreeNode* sortedArrayToBST(vector<int> A) {
return helper(A, 0, A.size() - 1);
}
int main()
{
vector<int> v;
v.push_back(1);
// v.push_back(2);
// v.push_back(2147483647);
cout<<sortedArrayToBST(v);
cout<<"1";
}