题目:用递归的方法输出以ABCD入栈的所有可能且合法的出栈顺序
方法借住二叉树的思想构成:
public class Allorder { static void all_order(String in,String stack,String out) { if(in.equals("")&&stack.equals("")) System.out.println(out); if(!in.equals("")) all_order(in.substring(0,in.length()-1),stack+in.charAt(in.length()-1),out); if(!stack.equals("")) all_order(in,stack.substring(0,stack.length()-1),out+stack.charAt(stack.length()-1)); } public static void main(String[] args) { String a=new String("ABCD"); all_order(a,"",""); } }