map<int, int> mapIndex;
void mapToIndex(int inorder[], int n)
{
for (int i = ; i < n; i++)
{
mapIndex.insert(map<int, int>::value_type(inorder[i], i);
}
}
Node* buildInorderPreorder(int in[], int pre[], int n, int offset)
{
assert(n >= );
if (n == )
return NULL;
int rootVal = pre[];
int i = mapIndex[rootVal] - offset;
Node* root = new Node(rootVal);
root->left = buildInorderPreorder(in, pre+, i, offset);
root->right = buildInorderPreorder(in+i+, pre+i+, n-i-, offset+i+);
return root;
}
Node* buildInorderPostorder(int in[], int post[], int n, int offset)
{
assert(n >= );
if (n == )
return NULL;
int rootVal = post[n-];
int i = mapIndex[rootVal] - offset;
Node* root = new Node(rootVal);
root->left = buildInorderPostorder(in, post, i, offset);
root->right = buildINorderPostorder(in+i+, post+i, n-i-, offset+i+);
return root;
}