该问题可以利用非递归的后序遍历加以修改一点即可完成:
void GetParent(BiTree Tree,char data,char Path[])
{
BiTree p = (BiTree)malloc(sizeof(tree));
BiTree IsPri = (BiTree)malloc(sizeof(tree));
int PathCount = 0;
IsPri = NULL;
p = Tree;
if(!Tree)
return;
Stack *stack;
InitStack(&stack);
while(!Empty(stack) || p)
{
while(p)
{
if(p->data == data)
{
while(!Empty(stack))
{
Path[PathCount++] = Pop(&stack)->data;
}
Path[PathCount] = '\0';
return;
}
Push(&stack,p);
p = p->lchild;
}
p = GetTop(stack);
if(p->rchild && p->rchild != IsPri)
p = p->rchild;
else
{
IsPri = Pop(&stack);
p = NULL;
}
}//end of while
return;
}