Advertisement
Guest User

Untitled

a guest
Aug 20th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 0.92 KB | None | 0 0
  1. private BSTNode removeRootOf(BSTNode subroot){
  2.         if (subroot.left ==null && subroot.right == null)
  3.            return null;
  4.         if(subroot.left == null || subroot.right == null){
  5.             if(subroot.left == null){
  6.                 return subroot.right;
  7.             }
  8.             else { return subroot.left;}
  9.         }
  10.  
  11.         BSTNode rightchild = subroot.right;
  12.  
  13.         if(rightchild.left == null){
  14.             rightchild.left = subroot.left;
  15.             return rightchild;
  16.  
  17.         }
  18.  
  19.         BSTNode parent = rightchild;
  20.  
  21.             while (parent.left.left != null) {
  22.             parent = parent.left;
  23.             }
  24.  
  25.                      BSTNode leftmostNode = parent.left;
  26.                         parent.left = leftmostNode.right;
  27.                         leftmostNode.right = rightchild;
  28.                          leftmostNode.left = subroot.left;
  29.                          return leftmostNode;
  30.  
  31.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement