contains category wise problems(data structures, competitive) of popular platforms.
View the Project on GitHub mayankdutta/category-wise-problems
key
matched,null
?left
right
current
one with the right
left
?leftmost
of the right
will be the smallest in so forming new Tree. TreeNode* dfs(TreeNode* root, int key) {
if (root == nullptr) {
return root;
}
if (root->val == key) {
if (root->right) {
auto temp = root->right;
while (temp->left != nullptr) temp = temp->left;
temp->left = root->left;
return root->right;
}
else {
return root->left;
}
}
root->left = dfs(root->left, key);
root->right = dfs(root->right, key);
return root;
}
TreeNode* deleteNode(TreeNode* root, int key) {
root = dfs(root, key);
return root;
}