title: C++ Delete a node from binary search tree
body: <p>This is the place where I am trying to solve this particular problem:
  <a href="http://mycodeschool.com/work-outs/binary-search-trees/7" rel="nofollow">http://mycodeschool.com/work-outs/binary-search-trees/7</a></p>
  
  <p>In case the node to be deleted has both children, the strategy to adopt is to replace that node with the maximum value in its left subtree (lets call it MAX_LEFT). Then you can simply delete the node MAX_LEFT. This strategy is also discussed in our video for this problem.</p>
  
  <p>I am trying to solve this problem by below code. But there are some issues and I am not getting the expected output. </p>
  
  <pre><code>struct Node
    {
       int data;
       struct Node* left;
       struct Node* right;
    }
  
  Node* FindMax(Node* root)
  {
      if(root==NULL)
      return NULL;
  
      while(root-&gt;right != NULL)
      {
          root = root-&gt;right;
      }
      return root;
  }
  Node* DeleteNodeInBST(Node* root,int data)
  {
      if(root==NULL) return root;
      else if(data&lt;=root-&gt;data) 
          root-&gt;left = DeleteNodeInBST(root-&gt;left, data);
      else if (data&gt; root-&gt;data)
          root-&gt;right = DeleteNodeInBST(root-&gt;right, data);
      else
      {
          //No child
          if(root-&gt;right == NULL &amp;&amp; root-&gt;left == NULL)
          {
              delete root;
              root = NULL;   
          }
          //One child 
          else if(root-&gt;right == NULL)
          {
              Node* temp = root;
              root= root-&gt;left;
              delete temp;
          }
          else if(root-&gt;left == NULL)
          {
              Node* temp = root;
              root= root-&gt;right;
              delete temp;
          }
          //two child
          else
          {
              Node* temp = FindMax(root-&gt;left);
              root-&gt;data = temp-&gt;data;
              root-&gt;left = DeleteNodeInBST(root-&gt;left, temp-&gt;data);
          }
      }
      return root;
  }
  </code></pre>