```
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
if(inorder == null || inorder.length ==0){
return null;
}
TreeNode root = new TreeNode(preorder[0]);
int leftEnd =-1;
int rightStart =inorder.length;
int leftInSize = 0;
for(int i=0; i<inorder.length; i++){
if(inorder[i] == root.val){
leftEnd = i-1;
rightStart = i+1;
break;
}
}
if(leftEnd >=0){
int[] leftIn = new int[leftEnd+1];
int[] leftPre = new int[leftIn.length];
System.arraycopy(inorder, 0, leftIn, 0, leftIn.length);
System.arraycopy(preorder,1,leftPre,0,leftPre.length);
root.left = buildTree(leftIn, leftPre);
leftInSize = leftIn.length;
}
if(rightStart < inorder.length){
int[] rightIn = new int[inorder.length - leftInSize -1];
int[] rightPre = new int[rightIn.length];
System.arraycopy(inorder, rightStart, rightIn, 0, rightIn.length);
System.arraycopy(preorder,1+leftInSize,rightPre,0,rightPre.length);
root.right = buildTree(rightIn, rightPre);
}
return root;
}
}
```

Wrong Answer:

```
Input: [1,2,3], [3,2,1]
Output: {1,3,#,2}
Expected: {1,2,#,3}
```

However, while I'm testing in local, the answer is the same as the expected one.

```
root:2left:3
root:1left:2
Result:1,2,#,3
```

I really appreciate any suggestion or solution on this problem!