forked from intrinsi/coding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortedArraytoBalancedBST.cpp
More file actions
79 lines (65 loc) · 1.51 KB
/
SortedArraytoBalancedBST.cpp
File metadata and controls
79 lines (65 loc) · 1.51 KB
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
// C++ program to print BST in given range
#include<bits/stdc++.h>
using namespace std;
/* A Binary Tree node */
class TNode
{
public:
int data;
TNode* left;
TNode* right;
};
TNode* newNode(int data);
/* A function that constructs Balanced
Binary Search Tree from a sorted array */
TNode* sortedArrayToBST(int arr[],
int start, int end)
{
/* Base Case */
if (start > end)
return NULL;
/* Get the middle element and make it root */
int mid = (start + end)/2;
TNode *root = newNode(arr[mid]);
/* Recursively construct the left subtree
and make it left child of root */
root->left = sortedArrayToBST(arr, start,
mid - 1);
/* Recursively construct the right subtree
and make it right child of root */
root->right = sortedArrayToBST(arr, mid + 1, end);
return root;
}
/* Helper function that allocates a new node
with the given data and NULL left and right
pointers. */
TNode* newNode(int data)
{
TNode* node = new TNode();
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
}
/* A utility function to print
preorder traversal of BST */
void preOrder(TNode* node)
{
if (node == NULL)
return;
cout << node->data << " ";
preOrder(node->left);
preOrder(node->right);
}
// Driver Code
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7};
int n = sizeof(arr) / sizeof(arr[0]);
/* Convert List to BST */
TNode *root = sortedArrayToBST(arr, 0, n-1);
cout << "PreOrder Traversal of constructed BST \n";
preOrder(root);
return 0;
}
// This code is contributed by rathbhupendra