btree *ancertor(btree*p,btree*q,btree *root)
{
btree* stack[MAX_SIZE],*s,*ancor[MAX_SIZE],*r;
int tag[MAX_SIZE],find;
int top=-1;
s=root;
while(s!=NULL)
{
top++;
stack[top]=s;
tag[top]=0;
s=s->left;
}
if(top>0)
{
s=stack[top];
if(tag[top]==1)
{
top--;
if(s==p)
{
for(int i=0;i<=top;i++)
ancor[i]=stack[i];
}
if(s==q)
{
int j=top;
while(j>0&&stack[j]!=ancor[j])j--;
if(j>0)
{
find =1;
r=ancor[j];
}
else
j--;
}
}
if(top>0)
{
s=s->right;
tag[top]=1;
}
}while(find||(s!=NULL&&top!=0));
return (r);
}
节点祖先
最新推荐文章于 2023-06-01 11:44:06 发布