二叉树深度问题

递归求解:

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;
}
原文地址:https://www.cnblogs.com/ma-liner/p/14196614.html