cxfans
8/29/2019 - 2:52 AM

p126-5

p126-5

#include <stddef.h>
#include <stdbool.h>

#define ElemType int
#define MaxSize 100

typedef struct BiTNode {
    ElemType data;
    struct BiTNode *lchild, *rchild;
} BiTNode, *BiTree;

int Depth(BiTree T) {
    if (!T) return 0;
    BiTree Q[MaxSize];
    BiTree p;
    int front = -1, rear = -1, level = 0, last = 0;
    Q[++rear] = T;
    while (front < rear) {
        p = Q[++front];
        if (p->lchild) Q[++rear] = p->lchild;
        if (p->rchild) Q[++rear] = p->rchild;
        if (front == last) {
            level++;
            last = rear;
        }
    }
    return level;
}