剑指offer(Java版)第五题:输入某二叉树的前序遍历和中序遍历的结果, 请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。 例如输入前序遍历序列{1, 2, 4, 7, 3, 5, 6, 8}和中序遍历序列{4, 7, 2, 1, 5, 3, 8, 6}, 则重建出其二叉树并输出它的头结点。

/*
输入某二叉树的前序遍历和中序遍历的结果,
请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。
例如输入前序遍历序列{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();
}
}

原文地址:https://www.cnblogs.com/zhuozige/p/12419111.html