-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminheightbst.cpp
More file actions
53 lines (42 loc) · 796 Bytes
/
minheightbst.cpp
File metadata and controls
53 lines (42 loc) · 796 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
44
45
46
47
48
49
50
51
52
53
#include <bits/stdc++.h>
using namespace std;
class BST
{
public:
int value;
BST*left;
BST*right;
BST(int value){
this->value = value;
left = nullptr;
right = nullptr;
}
};
BST* insert(vector<int>arr,int start,int end)
{
if(start>end)
{
return nullptr;
}
int mid = (start + end)/2;
BST*root = new BST(arr[mid]);
root->left = insert(arr,start,mid-1);
root->right = insert(arr,mid+1,end);
return root;
}
void display(BST* root)
{
if(root == nullptr)
{
return;
}
display(root->left);
cout<<root->value<<" ";
display(root->right);
}
int main()
{
vector<int>arr{1,2,5,7,10,13,14,15,22};
BST*root = insert(arr,0,arr.size()-1);
display(root);
}