重建二叉树中的盲区求教!!!
代码如下:
public class Solution {
public TreeNode reConstructBinaryTree(int[] pre,int[] in) {
TreeNode root = reConstructBinaryTree(pre,0,pre.length-1,in,0,in.length-1); //这条语句什么意思?TreeNode是接口吗?还有就是括号中的那么一大串是什么意思?
returnroot;
}
privateTreeNode reConstructBinaryTree(int[] pre,intstartPre,intendPre,int[] in,intstartIn,intendIn) { //这一条同问。
if(startPre>endPre||startIn>endIn)
returnnull;
TreeNode root = new TreeNode(pre[startPre]); //这里用new ,上面类似的语句怎么不用?
for(inti=startIn;i<=endIn;i++)
if(in[i]==pre[startPre]){
root.left=reConstructBinaryTree(pre,startPre+1,startPre+i-startIn,in,startIn,i-1);
root.right=reConstructBinaryTree(pre,i-startIn+startPre+1,endPre,in,i+1,endIn);
}
return root;
}
}
