Given a binary tree preorder traversal sequence in order traversal sequence, binary tree reconstruction.
//当前先序序列区间为【preL,preR】
//当前中序序列区间为【inL,inR】
//返回根结点地址
Node* create(int preL,int preR,int inL,int inR)
{
if(preL>preR) return NULL;
Node* root=new Node;
root->data=pre[preL];
int k;
for(k=inL;k<=inR;k++)
{
if(in[k]==pre[preL]) break;
}
int numLeft=k-inL;
root->lchild=create(preL+1,preL+numLeft,inL,k-1);
root->rchild=create(preL+numLeft+1,preR,k+1,inR);
return root;
}
Sequence found in the sequence in [k] [L] == pre nodes.
int k;
for(k=inL;k<=inR;k++)
{
if(in[k]==pre[preL]) break;
}