class Solution {
public int i = 0;
public TreeNode buildTree(int[] inorder, int[] postorder) {
i = postorder.length-1;
if(inorder == null || postorder == null){
return null;
}
return buildTreeChild(inorder,postorder,0,postorder.length-1);
}
public TreeNode buildTreeChild(int[] inorder,int[] postorder,
int inbegin,int inend){
if(inbegin > inend){
return null;
}
TreeNode root = new TreeNode(postorder[i]);
//逗号写成了点号,一定要注意细节
int rootIndex = findIndex(inorder,inbegin,inend,postorder[i]);
i--;
root.right = buildTreeChild(inorder,postorder,rootIndex + 1,inend);
root.left = buildTreeChild(inorder,postorder,inbegin,rootIndex - 1);
return root;
}
public int findIndex(int[] inorder,int begin,int inend,int key){
for(int i =begin;i<= inend;i++){
if(inorder[i] == key){
return i;
}
}
return -1;
}
}