PTA 求二叉树的深度
2024-10-11 20:52:44
6-7 求二叉树的深度 (6 分)
本题要求实现一个函数,可返回二叉树的深度。
函数接口定义:
int Depth(BiTree T);
T是二叉树树根指针,函数Depth返回二叉树的深度,若树为空,返回0。
裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>
typedef char ElemType;
typedef struct BiTNode
{
ElemType data;
struct BiTNode *lchild, *rchild;
}BiTNode, *BiTree;
BiTree Create();/* 细节在此不表 */
int Depth(BiTree T);
int main()
{
BiTree T = Create();
printf("%d\n", Depth(T));
return 0;
}
/* 你的代码将被嵌在这里 */
输出样例(对于图中给出的树):
4
int Depth(BiTree T){
if(T==NULL)
return 0;
int a=Depth(T->lchild);
int b=Depth(T->rchild);
if(a>b)
return a+1;
else
return b+1;
}
最新文章
- sql指定插入自增长id的数据
- iOS 学习 - 10下载(3) NSURLSession 音乐 篇
- mysql5.6启动占用内存很大的解决方法
- 终于有了自己的园子,Happy一下
- C#语言的Image和byte数组的互相转换
- BZOJ2375: 疯狂的涂色
- linux ssh 无密码登陆
- 慕课linux学习笔记(五)常用命令(2)
- Struts 2.3.4.1完整示例
- 连接linux主机
- iOS imageio nsurlsession 渐进式图片下载
- ActiveMQ与spring整合
- cocos creator 中的粒子效果
- mysql中文查询问题
- 【算法】LeetCode算法题-Two Sum
- 爬虫基础02-day24
- copy on write,代理模式
- Mybatis 事务管理和缓存机制
- Android 简单介绍图片压缩和图片内存缓存
- Azure 怎么开通FTP