-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gfg_hard_AVL_Tree_Deletion.cpp
83 lines (77 loc) · 1.7 KB
/
Gfg_hard_AVL_Tree_Deletion.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
/* Node is as follows:
struct Node
{
int data, height;
Node *left, *right;
Node(int x)
{
data = x;
height = 1;
left = right = NULL;
}
};
*/
//void inorder(Node* root, vector<int>&vrr)
// {
// if(root==NULL) return;
// inorder(root->left,vrr);
// vrr.push_back(root->data);
// inorder(root->right,vrr);
// }
// Node *buildTree(int data, vector<int>&vrr,int mid)
// {
// // -1 data indicates that we have the leaf node (Base Case)
// if (data == -1)
// {
// return NULL;
// }
// // Create the root node and hence solved 1 case
// Node *root = new Node(data);
// // Recursion will handle
// if(mid-1>=0){
// int leftData = vrr[mid-1];
// root->left = buildTree(leftData, vrr,mid);
// }
// if(mid+1<vrr.size()){
// int rightData= vrr[mid+1];
// root->right = buildTree(rightData,vrr,mid);
// }
// return root;
// }
// Node* deleteNode(Node* root, int data)
// {
// //add code here,
// vector<int>vrr;
// inorder(root,vrr);
// Node *root;
// int mid = vrr.size()/2;
// int data = vrr[mid];
// root = buildTree(data,vrr,mid);
// return root;
// }
vector<Node*>curr;
void inorder(Node* root, int data)
{
if(root==NULL) return;
inorder(root->left,data);
if(root->data!=data) curr.push_back(root);
inorder(root->right,data);
}
Node* build(int i, int j)
{
if(i>j) return NULL;
int mid = (i+j)/2;
Node* root = curr[mid];
root->left = build(i,mid-1);
root->right = build(mid+1,j);
return root;
}
Node* deleteNode(Node* root, int data)
{
//add code here,
curr.clear();
inorder(root,data);
int i=0;
int j = curr.size()-1;
return build(i,j);
}