62,614
社区成员
发帖
与我相关
我的任务
分享
public class NoRecuresive {
public static String move(String from, String to, String via, int num) {
TemplateX tx = new TemplateX();
for(int i = num - 1; i > 0; i--) {
tx = new TemplateX(tx, tx);
}
return tx.fill(from, to, via);
}
public static void main(String[] args) {
System.out.println(move("x", "y", "z", 4));
}
}
class TemplateX {
TemplateX l, r;
public TemplateX(){
l = r = null;
}
public TemplateX(TemplateX left, TemplateX right) {
l = left;
r = right;
if(l == null || r == null) {
throw new RuntimeException("Null");
}
}
public String fill(String from, String to, String via) {
if(l == null || r == null) {
return from + " -> " + to;
} else {
return l.fill(from, via, to) + "\n" + from + " -> " + to + "\n" + r.fill(via, to, from);
}
}
}