title: Split a binary search Tree
body: <p>Given a BST tree, we have to break the tree depending on the input(N), into two subtrees, where subtree1 consists of all the nodes, which are less than or equal to N and subtree2 consists of all the nodes which are greater than N.</p>
  
  <pre><code>          50
       /        \
       40         60
    /    \        /
   30     45     55
                   \
                    58
  </code></pre>
  
  <p>output:     </p>
  
  <pre><code>       50
        /                               
       40          
     /    \         
   30     45
  
  
       60
  
      /
  
     55
  
       \
  
         58
  </code></pre>
  
  <p>I have come up with following algorithm but its not working correctly:</p>
  
  <pre><code>static Node splitTree(Node root, Node root2, int k){
      if(root == null)
          return null;
  
      while(root != null){
          if(root.data &gt; k)
              root = root.left;
          else if(root.data &lt; k)
              root = root.right;
          else
          {
              root2=root.right;
              root.right=null;
              break;
          }
  
      }
          return root2;
  
  
  }
  </code></pre>