Answers for "If the node to be deleted has two children, please find the inorder successor of the node. Copy contents of the inorder successor to the node and delete the inorder successor"

C++
5

binary tree deletion

/* This is just the deletion function you need to write the required code.
	Thank you. */

void deleteNode(Node *root, int data)
{
    if(root == NULL)
    {
        cout << "Tree is emptyn";
        return;
    }

    queue<Node*> q;
    q.push(root);

    while(!q.empty())
    {
        Node *temp = q.front();
        q.pop();

        if(temp->data == data)
        {
            Node *current = root;
            Node *prev;

            while(current->right != NULL)
            {
                prev = current;
                current = current->right;
            }

            temp->data = current->data;
            prev->right = NULL;
            free(current);

            cout << "Deletedn";

            return;
        }

        if(temp->left != NULL)
            q.push(temp->left);
        if(temp->right != NULL)
            q.push(temp->right);
    }

    cout << "Node not found for deletionn";
}
Posted by: Guest on June-07-2020

Code answers related to "If the node to be deleted has two children, please find the inorder successor of the node. Copy contents of the inorder successor to the node and delete the inorder successor"

Browse Popular Code Answers by Language