# My java solution

•     /**
* 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 (preorder.length == 0) return null;
return recursive(preorder, inorder, 0, preorder.length-1, 0, preorder.length-1);
}

TreeNode recursive(int[] preorder, int [] inorder, int start1, int end1, int start2 , int end2) {
int rootVal = preorder[start1];
int idx = search(inorder, rootVal);

TreeNode n = new TreeNode(rootVal);
int len = idx - start2;

if (idx > start2) {
n.left = recursive(preorder, inorder, start1+1, start1+len, start2, idx-1);
}

if (idx < end2) {
n.right = recursive(preorder, inorder, start1+len+1, end1, idx+1, end2);
}

return n;
}

int search(int []a, int val) {
for(int i=0;i<a.length;i++)
if (a[i] == val) return i;
return -1;
}
}

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.