forked from Ashish-kumar7/geeks-for-geeks-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDelete a node from BST.cpp
More file actions
46 lines (43 loc) · 944 Bytes
/
Copy pathDelete a node from BST.cpp
File metadata and controls
46 lines (43 loc) · 944 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
Node *inorderSucc(Node *root)
{
Node *curr = root;
while(curr && curr->left!=NULL)
{
curr = curr->left;
}
return curr;
}
// Function to delete a node from BST.
Node *deleteNode(Node *root, int x) {
// your code goes here
if(root == NULL)
{
return root;
}
if(x < root->data)
{
root->left = deleteNode(root->left,x);
}
else if(x > root->data)
{
root->right = deleteNode(root->right,x);
}
else
{
if(root->left == NULL)
{
Node *temp = root->right;
free(root);
return temp;
}
else if(root->right == NULL)
{
Node *temp = root->left;
free(root);
return temp;
}
Node* temp = inorderSucc(root->right);
root->data = temp->data;
root->right = deleteNode(root->right, temp->data);
}
}