请编写函数,求二叉树的深度。
函数原型
int BinTreeDepth(const TNODE *root);
说明:root 为二叉树的根指针,函数值为二叉树的深度。
在头文件 BinTree.h 中声明函数,在程序文件 BinTree.c 中编写函数。
BinTree.h
…
int BinTreeDepth(const TNODE *root);
…
BinTree.c
…
/* 你提交的代码将被嵌在这里 */
裁判程序
main.c
#include <stdio.h>
#include "BinTree.h"
int main()
{
TNODE *r;
BinTreeCreate(&r);
BinTreeInput(&r);
printf("%d\n", BinTreeDepth(r));
BinTreeDestroy(&r);
return 0;
}
输入样例
EIBJ##H###DF#A##G#C##
输出样例
4
C:
int BinTreeDepth(const TNODE* root)
{
if (root == NULL)return 0;
int l = BinTreeDepth(root->lch);
int r = BinTreeDepth(root->rch);
int n = l < r ? r : l;
return n + 1;
}