4
down down up down down up up down down down up up down up up
题目中要求是从上到下的折痕的数组,那么就可以使用二叉树的中序遍历序列来列出所有的从上
到下的数组,代码如下:
import java.util.*;
public class FoldPaper {
public String[] foldPaper(int n) {
// write code here
ArrayList<String> list = new ArrayList<String>();
fold(list,1,n,true);
int m=list.size();
String[] put=new String[m];
int i=0;
for(String s:list){
put[i++]=s;
}
return put;
}
public static void fold(ArrayList<String> list,int cut,int n, boolean flag){
if(cut>n){
return ;
}
fold(list,cut+1,n,true);
String in=flag?"down":"up";
list.add(in);
fold(list,cut+1,n,false);
}
}