-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLC_450_DeleteFromBST.cpp
160 lines (140 loc) · 4.29 KB
/
LC_450_DeleteFromBST.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
https://leetcode.com/problems/delete-node-in-a-bst/
450. Delete Node in a BST
*/
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* parCurr = nullptr; // parent of current node
TreeNode* locCurr = nullptr; // location of current node
TreeNode* prev = nullptr;
// Search the location of node in the BST
int searchBST(TreeNode* t, int x)
{
parCurr = nullptr;
locCurr = nullptr;
prev = nullptr;
while(t)
{
if(x == t->val)
{
locCurr = t;
parCurr = prev;
return 1;
}
prev = t;
if(x < t->val){
t = t->left;
}
else{
t = t->right;
}
}// while
// if node not found
parCurr = prev;
locCurr = nullptr;
return -1;
}// end search BST
void caseA(TreeNode *par, TreeNode* loc, TreeNode** troot) // single or no children exists
{
TreeNode* child;
if(!loc->left && !loc->right)
child = nullptr;
else if(loc->left)
child = loc->left;
else
child = loc->right;
if(!par) // if parent is null
*troot = child; // single node, single node with either child
else
{
if(loc == par->left)
par->left = child;
else
par->right = child;
}
}// caseA
void caseB(TreeNode *par, TreeNode* loc, TreeNode** troot) // single or no children exists
{
TreeNode* suc, *parsuc, *ptr;
parsuc = loc;
suc = loc->right;
while(suc->left)
{
parsuc = suc;
suc = suc->left;
}
// delete successor node
if(suc->left && suc->right)
caseB(parsuc, suc, troot);
else
caseA(parsuc, suc, troot);
if(!par) // if parent is null
*troot = suc; // single node, single node with either child
else
{
if(loc == par->left)
par->left = suc;
else
par->right = suc;
}
suc->left = loc->left;
suc->right= loc->right;
}// caseB
TreeNode* deleteNode1(TreeNode* root, int key) {
if(!root) return root;
//Now Searching the location of Node to Delete.
int res = searchBST(root, key);
if(res == -1) return root;
if(locCurr->left && locCurr->right)
caseB(parCurr, locCurr, &root); // both the children exists
else
caseA(parCurr, locCurr, &root); // single or no children exists
locCurr = nullptr;
return root;
}// end
TreeNode* deleteNode(TreeNode* root, int key)
{
if(!root) return root;
if(key < root->val)
root->left = deleteNode(root->left, key);
else if(key > root->val)
root->right = deleteNode(root->right, key);
else
{
if(!root->left && !root->right) return nullptr;
if(!root->left) return root->right;
if(!root->right) return root->left;
else
{
TreeNode* p = root->right;
while(p->left)
p = p->left;
root->val = p->val;
root->right = deleteNode(root->right, root->val); // crawl right side with root val which is successors value also
}
}
return root;
}// end
};