二叉树前中序得后序
学妹问了就随手写一下……
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:
作者:火星十一郎
本文版权归作者火星十一郎所有,欢迎转载和商用,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.