Blow is the first solution I can give, which takes 15 ms. Looks like a terrible solution.

```
public TreeNode buildTree(int[] preorder, int[] inorder) {
return helper(preorder, 0, inorder, 0, inorder.length - 1);
}
public static TreeNode helper(int[] preorder, int preStart, int[] inorder, int inStart, int inEnd) {
if (preStart > preorder.length-1 || inStart > inEnd) {
return null;
}
TreeNode root = new TreeNode(preorder[preStart]);
int index= inStart;
while (inorder[index] != preorder[preStart]) {
index++;
}
root.left = helper(preorder, preStart + 1, inorder, inStart, index - 1);
root.right = helper(preorder, preStart + index - inStart + 1, inorder, index + 1, inEnd);
return root;
}
```

Then I checked the discussion other people posted. I found out that many good solutions tend to search from tail to head when searching for the index in inorder[]. So I try it in solution 1 and I get solution 2.

Here is the change on the base of solution 1:

```
int index= inEnd;
while (inorder[index] != preorder[preStart]) {
index--;
}
```

The running time turned out to be 1ms now! OMG! ! I am so confused because I thought the searching direction doesn't matter at all . Could anyone tell why. Thanks!