查找二叉树的任意节点的所有父节点

该问题可以利用非递归的后序遍历加以修改一点即可完成:

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;
}
    原文作者:二叉查找树
    原文地址: https://blog.csdn.net/c_living/article/details/79276706
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞