题目描述
二叉树的前序、中序、后序遍历的定义:
前序遍历:对任一子树,先访问跟,然后遍历其左子树,最后遍历其右子树;
中序遍历:对任一子树,先遍历其左子树,然后访问根,最后遍历其右子树;
后序遍历:对任一子树,先遍历其左子树,然后遍历其右子树,最后访问根。
给定一棵二叉树的前序遍历和中序遍历,求其后序遍历(提示:给定前序遍历与中序遍历能够唯一确定后序遍历)。
输入
两个字符串,其长度n均小于等于26。
第一行为前序遍历,第二行为中序遍历。
二叉树中的结点名称以大写字母表示:A,B,C....最多26个结点。
输出
输入样例可能有多组,对于每组测试样例,
输出一行,为后序遍历的字符串。
样例输入
ABC CBA ABCDEFG DCBAEFG
样例输出
CBA DCBGFEA
import java.util.Scanner; public class Main { private static boolean flag=false; public static class Node<E>{ public Node left; public Node right; public E e; public Node(E e) { this.e = e; } } public static Node<Integer> reBuild_BinaryTree(char[] preOrder, int preStart, int preEnd, char[] midOrder, int midStart, int midEnd){ if(preOrder.length!=midOrder.length||preOrder.length==0||midOrder.length==0) throw new RuntimeException("date not correct"); if(preOrder==null||midOrder==null) throw new RuntimeException("Null point Exception"); if(midStart>midEnd&&preStart>preEnd) return null; if(midStart==midEnd&&preStart==preEnd){ if(preOrder[preStart]!=midOrder[midStart]) { flag=true; return null; } return new Node(midOrder[midEnd]); } int mid; int pre; for (mid = midStart; mid < midEnd; mid++) { if (midOrder[mid] == preOrder[preStart]) break; } if(mid==midOrder.length){ flag=true; return null; } Node node = new Node(preOrder[preStart]); pre=preStart+(mid-midStart); node.left=reBuild_BinaryTree(preOrder,preStart+1,pre,midOrder,midStart,mid-1); node.right=reBuild_BinaryTree(preOrder,pre+1,preEnd,midOrder,mid+1,midEnd); return node; } public static void postOrder_print(Node head){ if(flag) return; if(head==null) return; postOrder_print(head.left); postOrder_print(head.right); System.out.print(head.e); } public static void main(String[] args) { Scanner cin=new Scanner(System.in); while(cin.hasNext()) { String str1 = cin.next(); char[] preOrder = str1.toCharArray(); String str2 = cin.next(); char[] midOrder = str2.toCharArray(); Node<Integer> head = reBuild_BinaryTree(preOrder, 0, preOrder.length - 1, midOrder, 0, midOrder.length - 1); postOrder_print(head); System.out.println(); } } }