程序填空题:计算二叉树深度
计算二叉树深度。
```c++
#include
using namespace std;
typedef struct BiNode
{
char data;
struct BiNode *lchild,*rchild;
}BiTNode,*BiTree;
void CreateBiTree(BiTree &T)
{
char ch;
cin >> ch;
if(ch=='#') T=NULL;
else{
T=new BiTNode;
T->data=ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
int Depth(BiTree T)
{
int m,n;
if(@@[T == NULL ](2)) return 0;
else
{
@@[m=Depth(T->lchild)](2);
@@[n=Depth(T->rchild)](2);
if(m>n) return(m+1);
else return (n+1);
}
}
int main()
{
BiTree tree;
CreateBiTree(tree);
cout< return 0;
}
```
答案:
第1空:T == NULL
第2空:m=Depth(T->lchild)
第3空:n=Depth(T->rchild)
```c++
#include
using namespace std;
typedef struct BiNode
{
char data;
struct BiNode *lchild,*rchild;
}BiTNode,*BiTree;
void CreateBiTree(BiTree &T)
{
char ch;
cin >> ch;
if(ch=='#') T=NULL;
else{
T=new BiTNode;
T->data=ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
int Depth(BiTree T)
{
int m,n;
if(@@[T == NULL ](2)) return 0;
else
{
@@[m=Depth(T->lchild)](2);
@@[n=Depth(T->rchild)](2);
if(m>n) return(m+1);
else return (n+1);
}
}
int main()
{
BiTree tree;
CreateBiTree(tree);
cout<
}
```
答案:
第1空:T == NULL
第2空:m=Depth(T->lchild)
第3空:n=Depth(T->rchild)