输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并输出它的后序遍历序列。
package com.huawei; import java.util.Scanner; class Node { Node left = null; Node right = null; int value; int size; } public class BinaryTreeBuilder { /** * 根据前序遍历和中序遍历重建二叉树子树 * @param preOrder 前序遍历数组 * @param start 子树起始位置 * @param inOrder 中序遍历数组 * @param end 中序遍历结束位置 * @param length 子树节点树 * @return 子树的根节点 */ public static Node buildTree(char[] preOrder, int start, char[] inOrder, int end, int length) { //参数验证 if (preOrder == null || preOrder.length == 0 || inOrder == null || inOrder.length == 0 || length <= 0) { return null; } //建立子树根节点 char value = preOrder[start]; Node root = new Node(); root.value = value; //递归终止条件:子树只有一个节点 if (length == 1) return root; //分拆子树的左子树和右子树 int i = 0; while (i < length) { if (value == inOrder[end - i]) { break; } i++; } //建立子树的左子树 root.left = buildTree(preOrder, start + 1, inOrder, end - i - 1, length - 1 - i); //建立子树的右子树 root.right = buildTree(preOrder, start + length - i, inOrder, end, i ); return root; } public static void getTree(Node root){ if(root == null){ System.out.println("No"); return; } if(root.left != null){ getTree(root.left); } if(root.right != null){ getTree(root.right); } System.out.print(root.value + " "); } public static Node rebuild(int[] preOrder, int startPre, int endPre, int[] inOrder, int startIn, int endIn){ if(endPre - startPre != endIn - startIn){ return null; } if(startPre > endPre){ return null; } Node root = new Node(); root.value = preOrder[startPre]; root.size = 1; root.left = null; root.right = null; if(startPre == endPre){ return root; } int index, length; for(index=startIn; index<=endIn; index++){ if(inOrder[index] == preOrder[startPre]){ break; } } if(index > endIn){ return null; } if(index > startIn){ length = index - startIn; root.left = rebuild(preOrder, startPre+1, startPre+length, inOrder, startIn, startIn+length-1); if(root.left != null) root.size += root.left.size; } if(index < endIn){ length = endIn - index; root.right = rebuild(preOrder, endPre-length+1, endPre, inOrder, endIn-length+1, endIn); if(root.right != null) root.size += root.right.size; } return root; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int n = sc.nextInt(); int[] preOrder = new int[n]; //{'1', '2', '4', '7', '3', '5', '6', '8'}; int[] inOrder = new int[n]; //{'4', '7', '2', '1', '5', '3', '8', '6'}; // String preStr = sc.next(); // System.out.println(preStr.length()); // String inStr = sc.nextLine(); for(int i=0; i<n; i++){ preOrder[i] = sc.nextInt();//.charAt(0); // inOrder[i] = inStr.charAt(i); } for(int i=0; i<n; i++){ // preOrder[i] = sc.next().charAt(0); inOrder[i] = sc.nextInt();//.charAt(0); } Node root = rebuild(preOrder,0,n-1,inOrder,0,n-1);//buildTree(preOrder, 0, inOrder, inOrder.length - 1, inOrder.length); if(root.size == n) getTree(root); else{ System.out.println("No"); } System.out.println(); } sc.close(); } }