Construct Binary Tree - Implementation

Here is the implementation:

function buildTree(preorder, inorder):
    inorderMap = map each value to its index in inorder
    return build(0, 0, inorder.length - 1)

function build(preStart, inStart, inEnd):
    if inStart > inEnd:
        return null

    rootVal = preorder[preStart]
    root = new TreeNode(rootVal)

    inRoot = inorderMap[rootVal]
    leftSize = inRoot - inStart

    root.left = build(preStart + 1, inStart, inRoot - 1)
    root.right = build(preStart + leftSize + 1, inRoot + 1, inEnd)

    return root

O(n)O(n) time with hashmap, O(n)O(n) space for the map and recursion stack.