题目:
输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。二叉树节点定义如下:
class BinaryTreeNode {
int data;
BinaryTreeNode left;
BinaryTreeNode right;
BinaryTreeNode(int data) {
this.data = data;
}
}
算法思想:
1.在二叉树的前序遍历中,第一个数字总是树的根节点的值。而在中序遍历中,根节点的值是在中间。
2.根据根节点的值,找出其在中序遍历中的位置。根节点的左边为左子树(假设节点个数为m,则前序遍历中根节点之后的m个节点为左子树,其余为右子树),根节点的右边为右子树。
3.找到了左、右子树的前序遍历和中序遍历,用同样的方法分别构建其左、右子树。可以用递归实现。
代码实现:
/**
*
* @param pre 前序遍历
* @param in 中序遍历
* @return 二叉树根节点
*/
public BinaryTreeNode reConstructBinaryTree(int[] pre,int[] in) {
/*
* 输入合法性判断, 不能为空,先序和后序长度要一致
*/
if(pre == null || in == null || pre.length != in.length)
return null;
return construct(pre, 0, pre.length-1, in, 0, in.length-1);
}
/**
*
* @param pre 前序遍历
* @param ps 前序遍历的开始位置
* @param pe 前序遍历的结束位置
* @param in 中序遍历
* @param is 中序遍历的开始位置
* @param ie 中序遍历的结束位置
* @return 数的根节点
*/
private BinaryTreeNode construct(int[] pre, int ps, int pe, int[] in, int is, int ie) {
//没有左子树或右子树
if(ps > pe) return null;
// 取前序遍历的第一个数字就是根节点
int value = pre[ps];
// 在中序遍历中中寻找根节点
int index =is;
while(index <= ie && value != in[index]) {
index ++;
}
// 如果在整个中序遍历的数组中没有找到,说明输入的参数是不合法的,抛出异常
if(index > ie)
throw new RuntimeException("Invalid Iuput!");
// 创建当前根节点,并为根节点赋值
BinaryTreeNode node = new BinaryTreeNode(value);
// 递归调用构建当前节点的左子树
node.left = construct(pre, ps+1, ps+index-is, in, is, index-1);
// 递归调用构建当前节点的右子树
node.right = construct(pre, ps+index-is+1, pe, in, index+1, ie);
return node;
}