重构二叉树

struct node

{

node*pleft;

node*pright;

int value;

}

voide rebuild(char *pre,char *pmid,int len ,node**root)

{

//检查边界条件

if(pre==null&&pmid==null||len<=0)

return;

//获得根节点

node*ptemp=new node;

ptemp->value=*pre;

ptemp->left=null;

ptemp->right=null;

if(*root==null)

{

*root=ptemp;

}

//找左右子树

int Ltemplen=0;

int *p=pmid;

while(*preorder!=*p)

{

p++;

Ltemplen++;     

}

int Rtemplen= len-1-Ltemplen;

//重建左子树

rebuild(pre+1,pmid,Ltemplen,&((*root)->left));

rebuild(pre+Ltemplen+1,pmid+Ltemplen+1,Rtemplen,&((*root)->right));

}

 

 

 

}

posted @ 2016-08-14 21:36  maxandhchen  阅读(140)  评论(0编辑  收藏  举报