Skip to content

Instantly share code, notes, and snippets.

@lectricas
Created April 22, 2021 14:44
Show Gist options
  • Save lectricas/50d8459894e718867ce3733e4ca0a0d9 to your computer and use it in GitHub Desktop.
Save lectricas/50d8459894e718867ce3733e4ca0a0d9 to your computer and use it in GitHub Desktop.
//
// Created by apolyusov on 19.04.2021.
//
#include "solution.h"
Node *my_method(Node *root) {
if (root->right == nullptr) {
return root;
}
return my_method(root->right);
}
Node *remove(Node *root, int key) {
if (root == nullptr) {
return root;
}
if (key > root->value) {
root->right = remove(root->right, key);
} else if (key < root->value) {
root->left = remove(root->left, key);
} else {
if (root->left == nullptr && root->right == nullptr) {
return nullptr;
}
if (root->left == nullptr) {
return root->right;
}
if (root->right == nullptr) {
return root->left;
}
Node *for_replace = my_method(root->left);
int temp = root->value;
root->value = for_replace->value;
for_replace->value = temp;
root->left = remove(root->left, key);
}
return root;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment