/*
輸入某二叉樹的前序遍歷和中序遍歷的結果,
請重建出該二叉樹。假設輸入的前序遍歷和中序遍歷的結果中都不含重復的數字。
例如輸入前序遍歷序列{1, 2, 4, 7, 3, 5, 6, 8}和中序遍歷序列{4, 7, 2, 1, 5, 3, 8, 6},
則重建出其二叉樹並輸出它的頭結點。
*/
import java.util.*;
public class Class6 {
class TreeNode{
int val;
public TreeNode(int val){
this.val = val;
}
TreeNode left;
TreeNode right;
}
public TreeNode reConstructBinaryTree(int[] pre, int[] in) {
if (pre == null || in == null || pre.length <= 0 || in.length <= 0 || pre.length != in.length) {
throw new RuntimeException("數據有誤!");
}
return construct(pre, in, 0, pre.length - 1, 0, in.length - 1);
}
public TreeNode construct(int[] pre, int[] in, int pStart, int pEnd, int iStart, int iEnd) {
TreeNode root = new TreeNode(pre[pStart]);
if (pStart == pEnd && iStart == iEnd) {
if (pre[pStart] != in[iStart])
throw new RuntimeException("數據有誤!");
return root;
}
int index = iStart; // 用於記錄中序遍歷序列中根結點的位置
while (root.val != in[index] && index <= iEnd) {
index++;
}
if (index > iEnd)
throw new RuntimeException("數據有誤!");
int leftLength = index - iStart;
if (leftLength > 0) {
root.left = construct(pre, in, pStart + 1, pStart + leftLength, iStart, index - 1);
}
if (leftLength < iEnd - iStart) {
root.right = construct(pre, in, pStart + leftLength + 1, pEnd, index + 1, iEnd);
}
return root;
}
private void pre1(TreeNode node){
if (node == null)
return;
System.out.print(node.val);
pre1(node.left);
pre1(node.right);
}
private void in1(TreeNode node){
if (node == null)
return;
in1(node.left);
System.out.print(node.val);
in1(node.right);
}
private void test(){
int[] pre = {1, 2, 4, 7, 3, 5, 6, 8};
int[] in = {4, 7, 2, 1, 5, 3, 8, 6};
TreeNode root = reConstructBinaryTree(pre, in);
System.out.println("前序遍歷:");
pre1(root);
System.out.println();
System.out.println("中序遍歷:");
in1(root);
}
public static void main(String[] args) {
Class6 c = new Class6();
c.test();
}
}