public static TreeNode sub_flat (TreeNode root) {//AC
        if (root == null) return null;
        else if (root.left == null && root.right == null) return root;
        else {
            TreeNode tempr = sub_flat(root.right);
            root.right = sub_flat(root.left);
            root.left = null;
            TreeNode temp = root;
            while (temp.right != null) {
                temp = temp.right;
            }
            temp.right = tempr;
            return root;
        }
    }