Mirror a B-Tree

public class BinarySearchTree {
 
 public class Node {
  private int data;
  private Node leftChild, rightChild;
 }

 public static Node mirrorBST(Node node) {
   
  if (node.equals(null)) return(node);
  
  // swap children of this node
  Node tmp;
  tmp = node.leftChild;
  node.leftChild = node.rightChild;
  node.rightChild = tmp;
   
  // do the subtrees - RECURSSION
  if (node.leftChild != null) {
   mirrorBST(node.leftChild);
  }
  if (node.rightChild != null) 
   mirrorBST(node.rightChild);
   
  return (node);
 }
}

0 comments: