功能从构建树序和预购无法理解这个代码的最后一步
struct treenode * construct(struct listnode *inptr,struct listnode * preptr ,int num)
{
struct treenode *temp;
struct listnode *q;
int i,j;
if(num==0)
return NULL;
temp=(struct treenode *)malloc(sizeof(struct treenode));
temp->info=preptr->info;
temp->left=NULL;
temp->right=NULL;
if(num==1)/*if only one node in tree*/
return temp;
q=inptr;
for(i=q;q->info!=preptr->info;i++) q=q->next;
/*now q points to root node in inorder list and the number of nodes in its left tree is i*/
/*for left subtree*/
temp->lchild=construct(inptr,preptr->next,i);
/*for right subtree*/
for(j=1;j<=i+1;j++) preptr=preptr->next;
temp->rchild=construct(q->next,preptr,num-i-1);//unbale to understand this step as after node G construction value of i and num would be same this would result in -1
return temp;
}/*end of construct */
问题:我不明白我们想通过temp->rchild=construct(q->next,preptr,num-i-1);
你究竟知道些什么?语法?逻辑?请更具体一些。 – Maroun 2014-12-08 08:57:44
它递归地调用该函数。 – 2014-12-08 08:59:45
@MarounMaroun逻辑不清楚,因为后手调试我发现i和num的值将在某一步是相同的,并且不会是终止的基本情况我在数据结构书中发现此代码 – yogeshmanjhi 2014-12-08 09:01:22