-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathp12.cpp
59 lines (50 loc) · 1.42 KB
/
p12.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
Node* deleteNode(Node* root, int k) {
if (root == NULL)
return root;
if (root->data > k) {
root->left = deleteNode(root->left, k);
return root;
}
else if (root->data < k) {
root->right = deleteNode(root->right, k);
return root;
}
// We reach here when root is the node
// to be deleted.
// If one of the children is empty
if (root->left == NULL) {
Node* temp = root->right;
delete root;
return temp;
}
else if (root->right == NULL) {
Node* temp = root->left;
delete root;
return temp;
}
// If both children exist
else {
Node* succParent = root;
// Find successor
Node* succ = root->right;
while (succ->left != NULL) {
succParent = succ;
succ = succ->left;
}
// Delete successor. Since successor
// is always left child of its parent
// we can safely make successor's right
// right child as left of its parent.
// If there is no succ, then assign
// succ->right to succParent->right
if (succParent != root)
succParent->left = succ->right;
else
succParent->right = succ->right;
// Copy Successor Data to root
root->data = succ->data;
// Delete Successor and return root
delete succ;
return root;
}
}