找寻路径从根root到节点p的路径函数

来源:互联网 发布:中英文在线翻译软件 编辑:程序博客网 时间:2024/05/29 16:27
//从根root到节点p的路径函数void Path(btree *root,btree *p){    btree *stack[MAX_SIZE],*b;    int tag[MAX_SIZE];//标记左右孩子    int top=-1,find=0;    b=root;    while(b!=NULL)    {        top++;        stack[top]=b;        tag[top]=0;        b=b->left;    }    if(top>0)    {        if(tag[top]==1)        {            b=stack[top];            if(b==p)            {                for(int i=0;i<=top;i++)                {                    cout<<stack[i]->data;                    find=1;                }            }            else top--;        }        if(top>0&&!find)        {            p=p->right;            tag[top]=1;        }    }while(find||(b!=NULL&&top!=0));}
0 0
原创粉丝点击