why local variable changed automatically : <p>I write a function to delete a node in binary search tree. 
Here is my code.</p>

<pre><code>#include &lt;iostream&gt;
struct BSTNode
{
    int data;
    BSTNode *left;
    BSTNode *right;
};

BSTNode * GetNode(int data);
BSTNode * FindMin(BSTNode *root);
BSTNode * Insert(BSTNode *root, int data);
void PreOrder(BSTNode *root);
BSTNode * DeleteNode(BSTNode *root,int data);

BSTNode * GetNode(int data)
{
    BSTNode *newNode = new BSTNode;
    newNode-&gt;data = data;
    newNode-&gt;left = NULL;
    newNode-&gt;right = NULL;

    return newNode;
}

BSTNode * FindMin(BSTNode *root)
{
    if(root == NULL)
        return root;
    while(root-&gt;left != NULL)
        root = root-&gt;left;
    return root;

}

BSTNode * Insert(BSTNode *root, int data)
{
    if(root == NULL)
    {
        root = GetNode(data);
        return root;
    }
    else if (data &lt;= root-&gt;data)
        root-&gt;left = Insert(root-&gt;left, data);
    else if (data &gt; root-&gt;data)
        root-&gt;right = Insert(root-&gt;right, data);
    return root;
}

void PreOrder(BSTNode *root)
{
    if(root == NULL)
        return;
    std::cout &lt;&lt; root-&gt;data &lt;&lt; std::endl;
    PreOrder(root-&gt;left);
    PreOrder(root-&gt;right);
}

BSTNode * DeleteNode(BSTNode *root,int data)
{
    if(root == NULL)
        return root;
    else if(data &lt; root-&gt;data)
        root-&gt;left = DeleteNode(root-&gt;left, data);
    else if(data &gt; root-&gt;data)
        root-&gt;right = DeleteNode(root-&gt;right, data);
    else // Found the Node
    {
        // case 1: No child
        if(root-&gt;left == NULL &amp;&amp; root-&gt;right == NULL)
        {
            delete root;
            root = NULL;
            return root;
        }
        // case 2: One child
        else if(root-&gt;left == NULL)
        {
            BSTNode * temp = root;
            root = root-&gt;right;
            delete temp;
            return root;
        }
        else if(root-&gt;right = NULL)
        {
            BSTNode * temp = root;
            root = root-&gt;left;
            delete temp;
            return root;
        }
        else // case 3: two child
        {
            BSTNode * temp = FindMin(root-&gt;right);
            root-&gt;data = temp-&gt;data;
            root-&gt;right = DeleteNode(root-&gt;right, temp-&gt;data);
        }
    }
}


int main()
{
    BSTNode *root = NULL;
    root = Insert(root, 15);
    root = Insert(root, 10);
    root = Insert(root, 20);
    root = Insert(root, 25);
    root = Insert(root, 8);
    root = Insert(root, 12);
    root = DeleteNode(root, 15);
    PreOrder(root);

    return 0;
}
</code></pre>

<p>but i across a problem with </p>

<pre><code>else // case 3: two child
        {
            BSTNode * temp = FindMin(root-&gt;right);
            root-&gt;data = temp-&gt;data;
            root-&gt;right = DeleteNode(root-&gt;right, temp-&gt;data);
        }
</code></pre>

<p>when program came to else statement, root->right become NULL automatically</p>

<p>why this happen ? how can i fix it ?</p>

<p>any ideas appreciated!</p>
