import sun.reflect.generics.tree.Tree;

/**
 * Created by Administrator on 2017/3/2.
 */

class TreeNode {
    public int val;
    public TreeNode left;
    public TreeNode right;

    TreeNode () {
        this.val = 0;
        this.left = null;
        this.right = null;
    }

    TreeNode (int val) {
        this.val = val;
        this.left = null;
        this.right = null;
    }
}

public class ConstructBinaryTree {

    public TreeNode contrust (int[] preOrder, int[] inOrder) {
        if (preOrder == null || inOrder == null || preOrder.length <= 0)
            return null;
        return contrustCore(preOrder, 0, preOrder.length-1, inOrder, 0, inOrder.length-1);
    }

    private TreeNode contrustCore (int[] preOrder, int startPreOrder, int endPreOrder, int[] inOrder, int startInOrder, int endInOrder) {
        int rootValue = preOrder[startPreOrder];
        TreeNode root = new TreeNode(rootValue);

        if (startPreOrder == endPreOrder)
            if (startInOrder == endInOrder && startPreOrder == startInOrder)
                return root;

        int rootInorder = startInOrder;
        while (rootInorder <= endInOrder && inOrder[rootInorder] != rootValue)
            ++rootInorder;

        int leftLength = rootInorder - startInOrder;
        int leftPreOrderEnd = startPreOrder + leftLength;

        if (leftLength > 0)
            root.left = contrustCore(preOrder, startPreOrder+1, leftPreOrderEnd ,inOrder, startInOrder, rootInorder-1);
        if (leftLength < endPreOrder - startPreOrder)
            root.right = contrustCore(preOrder, leftPreOrderEnd+1, endPreOrder, inOrder, rootInorder+1, endInOrder);
        return root;
    }

    public void preOrder (TreeNode root) {
        if (root == null) return;
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }

    public void inOrder (TreeNode root) {
        if (root == null) return;
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }

    public void afterOrder (TreeNode root) {
        if (root == null) return;
        afterOrder(root.left);
        afterOrder(root.right);
        System.out.print(root.val + " ");
    }

    public static void main(String[] args) {
        int[] preOrder = {1,2,4,7,3,5,6,8};
        int[] inOrder = {4,7,2,1,5,3,8,6};

        ConstructBinaryTree c = new ConstructBinaryTree();

        TreeNode res = c.contrust(preOrder, inOrder);

        c.preOrder(res);
        System.out.println();
        c.inOrder(res);
        System.out.println();
        c.afterOrder(res);
    }
}

@@ -1,46 +1,4 @@
-import sun.reflect.generics.tree.Tree;%0D%0A%0D%0A
 /**%0D
@@ -43,16 +43,1057 @@
 /2.%0D%0A */
+%0D%0A%0D%0A// good solution%0D%0A// leetcode_105%0D%0Apublic class Solution %7B%0D%0A    public TreeNode buildTree(int%5B%5D preorder, int%5B%5D inorder) %7B%0D%0A        return helper(0, preorder.length - 1, 0, inorder.length - 1, preorder, inorder);%0D%0A    %7D%0D%0A    private TreeNode helper(int preStart, int preEnd, int inStart, int inEnd, int%5B%5D preorder, int%5B%5D inorder) %7B%0D%0A        if(preStart %3E preEnd %7C%7C inStart %3E inEnd) return null;%0D%0A        TreeNode root = new TreeNode(preorder%5BpreStart%5D);%0D%0A        int edge = 0;%0D%0A        for (int i = inStart, j = inEnd; i %3C= j; i++, j--) %7B%0D%0A            if (inorder%5Bi%5D == root.val) %7B%0D%0A                edge = i;%0D%0A                break;%0D%0A            %7D%0D%0A            if (inorder%5Bj%5D == root.val) %7B%0D%0A                edge = j;%0D%0A                break;%0D%0A            %7D%0D%0A        %7D%0D%0A        int leftLen = edge - inStart;%0D%0A        root.left = helper(preStart + 1, preStart + leftLen, inStart, edge - 1, preorder, inorder);%0D%0A        root.right = helper(preStart + 1 + leftLen, preEnd, edge + 1, inEnd, preorder, inorder);%0D%0A        return root;%0D%0A    %7D%0D%0A%7D
 %0D%0A%0D%0Aclas

Create 006_ConstructBinaryTree.java