递归左子树,参数preEnd要传入:preStart+i-inStart,这个为什么要减inStart
递归右子树参数preStart要传入:preStart+i-inStart+1,这个为什么要减inStart
private static TreeNode reConstructBinaryTree(int[] pre,int preStart,int preEnd,int[] in,int inStart,int inEnd){
if(preStart>preEnd || inStart > inEnd){
return null;
}
TreeNode treeNode =new TreeNode(pre[preStart]);
for(int i=inStart;i<=inEnd;i++){
if(in[i] == pre[preStart]){
treeNode.lchild = reConstructBinaryTree( pre,preStart+1,preStart+i-inStart,in,inStart,i-1);
treeNode.rchild = reConstructBinaryTree(pre,preStart+i-inStart+1,preEnd,in,i+1,inEnd);
}
}
return treeNode;
}