I would like to ask for corrections on that function. The problem is that it deletes more nodes than there are leaves. Means it is deleting all the leaves first, and then it is deleting the just created leaves. Please, advise me how to correct it. Leaf = node without children nodes.
typedef struct node {
int key;
node* left;
node* right;
}node;
void BST::DeleteLeafs()
{
if (root == nullptr)
throw InvalidOperationException();
else
deleteLeavesHelper(nullptr, root);
}
void BST::deleteLeavesHelper(node* parent, node* n)
{
if (n == nullptr)
return;
else
{
if (n->left == nullptr && n->right == nullptr)
{
n = nullptr;
if (parent != nullptr)
{
parent->left = nullptr;
parent->right = nullptr;
}
}
else
{
deleteLeavesHelper(n, n->left);
deleteLeavesHelper(n, n->right);
}
}
}