二叉树前中序得后序

学妹问了就随手写一下……

  1: /*
  2:  * 自己写的,以前C++写过,找不到了,学妹问我,就再写了一次
  3:  * 二叉树前中序得后序
  4:  */
  5: public class PostOrder {
  6: 
  7:   public static void main(String[] args) {
  8:     String s1 = "abdec";
  9:     String s2 = "dbeac";
 10:     outPut(s1,s2);
 11:     System.out.println();
 12:   }
 13: 
 14:   private static void outPut(String s1, String s2) {
 15:     //此时s2长度也是1,内容和s1一样
 16:     if(s1.length()==1) {
 17:       System.out.print(s1+" ");
 18:       return ;
 19:     }
 20:     char root = s1.charAt(0);
 21:     int index1 = s2.indexOf(root);
 22:     char ch = s2.charAt(index1-1);
 23:     int index2 = s1.indexOf(ch);
 24:     outPut(s1.substring(1,1+index1), s2.substring(0, index1));
 25:     outPut(s1.substring(index2+1), s2.substring(index1+1));
 26:     //不加的话少输出根
 27:     System.out.print(root+" ");
 28:   }
 29: }
 30: 

你可能感兴趣的:(二叉树)