二叉树深度问题

递归求解:

int SECUR_BEDEPTH(BTREE T)
{
    int ldepth=0,rdepth;
    if(T){
        ldepth=BEDEPTH(T->lchild);
        rdepth=BEDEPTH(T->rchild);
        return ldepth>rdepth? ldepth+1:rdepth+1;
    }
    return 0;
}

非递归求解:

int BEDEPTH(BTREE T)
{
    BTREE stack[MAXSIZE],p = T;
    int stack1[MAXSIZE];
    int curdepth,maxdepth, top = -1;
    if(T){
        curdepth = 1;
        do{
            while(p){
                stack[++top] = p;
                stack1[top] = curdepth;
                p = p->lchild;
                curdepth++;
            }
            p = stack[top];
            curdepth = stack1[top--];
            if(!(p->rchild||p->lchild)&&maxdepth<curdepth)
                maxdepth  = curdepth;
            p = p->rchild;
            curdepth++;
        }while(!p&&top == -1);
    }
    return maxdepth;
}
posted @ 2019-05-17 19:45  pony.ma  阅读(51)  评论(0编辑  收藏  举报