前序,中序递归得到后序遍历算法
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static String res = "";
static int findchar(String s,char a){ //s中a出现的位置
for(int i=0;i<s.length();i++){
if(a==s.charAt(i)) return i; }
return -1;
}
static void cal_tree(String sa,String sb){
if(sb.length()==0) return ;
if(sb.length()==1){ res+=sb; return ; }
char x = sa.charAt(0);
int mid = findchar(sb,x);
String c = sb.substring(0,mid);
String d = sb.substring(mid+1);
cal_tree(sa.substring(1,c.length()+1),c); //递归左子树
cal_tree(sa.substring(1+c.length()),d); //递归右子树
res+=String.valueOf(x);
return ;
}
public static void main(String[] agrs){
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while(true){
String s1 = br.readLine().trim();
String s2 = br.readLine().trim();
cal_tree(s1,s2);
System.out.println(res);
res=""; }
} catch (Exception e) { }
}