-
Notifications
You must be signed in to change notification settings - Fork 0
/
108 Convert Sorted Array to Binary Search Tree.c
96 lines (84 loc) · 2.14 KB
/
108 Convert Sorted Array to Binary Search Tree.c
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
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
int height(struct TreeNode * root)
{
if(root == NULL)
return 0;
int left = height(root->left);
int right = height(root->right);
if(left >= right)
return left+1;
return right+1;
}
int calculate_bf(struct TreeNode * root)
{
int balance_factor = 0;
if(root->left == NULL && root->right == NULL)
{
balance_factor = 0;
}
else if(root->left == NULL && root->right != NULL)
{
balance_factor = 0 - (height(root->right));
}
else if(root->left != NULL && root->right == NULL)
{
balance_factor = height(root->left);
}
else
{
balance_factor = height(root->left) - height(root->right);
}
return balance_factor;
}
struct TreeNode * r_insert(struct TreeNode * root, int element)
{
if(root == NULL)
{
struct TreeNode * new_node = (struct TreeNode *)malloc(sizeof(struct TreeNode));
new_node->val = element;
new_node->left = NULL;
new_node->right = NULL;
return new_node;
}
else
{
if(element < root->val)
{
root->left = r_insert(root->left, element);
}
else if(element > root->val)
{
root->right = r_insert(root->right, element);
}
//Calculate the balance factor
int balance_factor = calculate_bf(root);
if(balance_factor >= 2 || balance_factor <= -2)
{
//Only RR Rotation is required
struct TreeNode * tmp_ptr = root;
struct TreeNode * right_ptr = root->right;
tmp_ptr->right = right_ptr->left;
right_ptr->left = tmp_ptr;
return right_ptr;
}
}
return root;
}
struct TreeNode* sortedArrayToBST(int* nums, int numsSize)
{
int i=0;
struct TreeNode * root = NULL;
//Insert the nodes in the tree
for(i=0; i<numsSize; i++)
{
root = r_insert(root, nums[i]);
}
return root;
}