输入二叉树为:ABD##E##C##。
运行环境:main.cpp
运行结果:3
#include "bits/stdc++.h"
using namespace std;
typedef struct BiTNode{
char data;
struct BiTNode *lchild,*rchild;
int tag;
}BiTNode,*BiTree;
void createTree(BiTree &t){
char ch;
ch=getchar();
if (ch=='#') t=NULL;
else{
t=(BiTNode *) malloc(sizeof (BiTNode));
t->data=ch;
t->tag=0;
t->lchild=NULL;
t->rchild=NULL;
createTree(t->lchild);
createTree(t->rchild);
}
}
int btDepth(BiTree t){
if (!t)
return 0;
int front=-1,rear=-1;
int last=0,level=0;
BiTree Q[100];
Q[++rear]=t;
BiTree p;
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;
}
int main() {
BiTree t;
createTree(t);
printf("%d", btDepth(t));
}