-->
当前位置:首页 > 题库 > 正文内容

程序填空题:计算二叉树的深度

Luz4年前 (2021-05-10)题库4380
使用递归方式计算二叉树的深度。提示:二叉树是根据先序遍历顺序建立起来的。

```c
#include
#include
typedef struct BiNode
{
char data;
struct BiNode *lchild, *rchild;
}BiTNode, *BiTree;


void CreateBiTree(BiTree * T)
{
char ch;
scanf("%c", &ch);
if (ch == '#')
*T = NULL;
else
{
*T = (BiTree)malloc(sizeof(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 = NULL;
CreateBiTree(&tree);
int depth = Depth(tree);
printf("%d", depth);
return 0;
}
```
**测试数据**





输入:ab##c##

输出:2





答案:
第1空:T == NULL

第2空:Depth(T->lchild)

第3空:Depth(T->rchild)

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。