package leetcode;

/**
 * https://leetcode.com/problems/increasing-order-search-tree/
 */
public class Problem897 {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    public TreeNode increasingBST(TreeNode root) {
        TreeNodeRef ref = new TreeNodeRef();
        increasingBST(root, ref);
        return ref.root;
    }

    private static class TreeNodeRef {
        private TreeNode root;
        private TreeNode node;
    }

    private static void increasingBST(TreeNode root, TreeNodeRef ref) {
        if (root == null) {
            return;
        }
        increasingBST(root.left, ref);
        if (ref.root == null) {
            ref.root = root;
            ref.node = root;
        } else {
            ref.node.left = null;
            ref.node.right = root;
            ref.node = root;
        }
        increasingBST(root.right, ref);
    }

    private static void print(TreeNode node) {
        for (TreeNode n = node; n != null; n = n.right) {
            if (n.left != null) {
                throw new RuntimeException("Wrong!");
            }
            System.out.println(n.val);
        }
    }

    public static void main(String[] args) {
        Problem897 prob = new Problem897();
        TreeNode root = new TreeNode(5);
        root.left = new TreeNode(3);
        root.left.left = new TreeNode(2);
        root.left.right = new TreeNode(4);
        root.left.left.left = new TreeNode(1);
        root.right = new TreeNode(6);
        root.right.right = new TreeNode(8);
        root.right.right.left = new TreeNode(7);
        root.right.right.right = new TreeNode(9);

        print(prob.increasingBST(root));
        print(prob.increasingBST(new TreeNode(1)));
    }
}

@@ -265,32 +265,458 @@
        %7D%0A    %7D%0A%0A
+//    TreeNode cur;%0A//    public TreeNode increasingBST(TreeNode root) %7B%0A//        TreeNode ans = new TreeNode(0);%0A//        cur = ans;%0A//        inorder(root);%0A//        return ans.right;%0A//    %7D%0A//%0A//    public void inorder(TreeNode node) %7B%0A//        if (node == null) return;%0A//        inorder(node.left);%0A//        node.left = null;%0A//        cur = node;%0A//        cur.right = cur;%0A//        inorder(node.right);%0A//    %7D%0A%0A
     public TreeN
@@ -1016,20 +1016,20 @@
 reeNode 
-root
+node
 , TreeNo
@@ -1053,20 +1053,20 @@
     if (
-root
+node
  == null
@@ -1121,20 +1121,20 @@
 singBST(
-root
+node
 .left, r
@@ -1189,28 +1189,28 @@
  ref.root = 
-root
+node
 ;%0A          
@@ -1214,36 +1214,36 @@
      ref.node = 
-root
+node
 ;%0A        %7D else
@@ -1308,20 +1308,20 @@
 right = 
-root
+node
 ;%0A      
@@ -1337,20 +1337,20 @@
 .node = 
-root
+node
 ;%0A      
@@ -1375,20 +1375,20 @@
 singBST(
-root
+node
 .right, 

Update problem 897