写了个练手,求牛人指正
public static TreeNode rebuild(int[] inorder,int[] bfs,int bfsl,int bfsh,int
inorderl, inorderh){
//base case
if(bsfl>=bfsh||inorderl>inorderh) return null;
TreeNode root=new TreeNode();
root.value=bfs[bfsl];
//find the index of root value in inorder array
int cutoff=findCut(inorder,inorderl,inorderh,bfs[bfsl]);
//build the tree recursively
root.right=rebuild(inorder,bfs,bfsl+1,bfsh,inorderl,cutoff-1);
root.left=rebuild(inorder,bfs,bfsl+2,bfsh,cutoff+1,inorderh);
return root;
}
public static int findCut(int[] array, int l, int h, int x){
for(int i=0;i<=h,i++){
if(array[i]==x)
return i;
}
return -1;
}
//API call
//rebuild(inorder,bfs,0,bfs.length,0,inorder.length-1);