12.在二叉树中查找值为x的结点,打印值为x的结点的所有祖先,假设值为x的结点不多于一个

bool ancestor(Bitree bt,Elemtype x)
{
    if(bt==NULL) //递归出口
        return false;
    else if(bt->lchild!=NULL&&bt->rchild->data==x||bt->rchild->data==x&&bt->lchild!=NULL)
    { //结点的左孩子或者右孩子的data为x
        printf("%c",bt->data);
        return true;
    }
    else if(ancestor(bt->lchild,x)||ancestor(bt->rchild,x))
    {
        printf("%c",bt->data);
        return true;
    }
    else
         return false;
}
View Code
原文地址:https://www.cnblogs.com/spore/p/11623614.html