Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Node {
- Node left;
- Node right;
- Integer value;
- public Node getRightmostDescendant() {
- Node rightmost = this;
- while (rightmost.right != null) {
- rightmost = rightmost.right;
- }
- return rightmost;
- }
- }
- public class BinaryTree {
- Node root;
- public void toFlatten(Node node) {
- if (node == null) {
- return;
- }
- if (node.left != null) {
- Node moving = node.left;
- Node rightmost = moving.getRightmostDescendant();
- Node currentRight = node.right;
- node.right = moving;
- rightmost.right = currentRight;
- }
- toFlatten(node.right);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement