Advertisement
diegocedrim

Untitled

Nov 16th, 2014
800
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.59 KB | None | 0 0
  1. class Node {
  2.     Node left;
  3.     Node right;
  4.     Integer value;
  5.  
  6.     public Node getRightmostDescendant() {
  7.         Node rightmost = this;
  8.         while (rightmost.right != null) {
  9.             rightmost = rightmost.right;
  10.         }
  11.         return rightmost;
  12.     }
  13.  
  14. }
  15.  
  16. public class BinaryTree {
  17.     Node root;
  18.  
  19.     public void toFlatten(Node node) {
  20.         if (node == null) {
  21.             return;
  22.         }
  23.         if (node.left != null) {
  24.             Node moving = node.left;
  25.             Node rightmost = moving.getRightmostDescendant();
  26.  
  27.             Node currentRight = node.right;
  28.             node.right = moving;
  29.             rightmost.right = currentRight;
  30.         }
  31.         toFlatten(node.right);
  32.     }
  33.  
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement