Hello everyone. I would like to know why the function below would need a pointer to the pointer, in this case a **root
, so that the line / 1 /
could modify the *root
, while the line / 2 / can modify it without having to resort to it. What is the difference between:
root->right = remover(root->right,valor);
and
root = NULL;
?
Are the two not trying to modify the pointer without using a pointer? So the two should not be wrong?
FUNCTION:
Node * remover(Node *root, int valor){
if(root == NULL){
cout<<"Elemento não encontrado."<<endl;
}
else{
if(root->data == valor){
root = NULL;
cout<<"Elemento removido!"<<endl;
return root;
}
if(root->data > valor){
/* 1 */ //remover(root->left,valor); não funciona
/* 2 */root->left = remover(root->left,valor); // funciona
return root;
}
if(root->data < valor){
/* 1 */ //remover(root->right,valor); não funciona
/* 2 */root->right = remover(root->right,valor); // funciona
return root;
}
}
}