public TreeNode ConstructBinaryTree(int [] pre,int [] in) {
TreeNode root=reConstructBinaryTree(pre,0,pre.length-1,in,0,in.length-1);
return root;
}
public TreeNode reConstructBinaryTree(int[] pre,int prestart,int preend,
int[] in,int instart,int inend){
TreeNode root=new TreeNode(pre[prestart]);
if(prestart>preend||instart>inend){
return null;
}
for(int i=instart;i<=inend;i++){
if(pre[prestart]==in[i]){
root.left=reConstructBinaryTree(pre,prestart+1,prestart+i-instart,
in,instart,i-1);
root.right=reConstructBinaryTree(pre,prestart+i-instart+1,preend,
in,i+1,inend);
break;
}
}
return root;
}
根据前序遍历中序遍历求二叉树
最新推荐文章于 2021-04-18 14:00:35 发布