(015)实现一个函数来检查是否平衡树(keep it up)
2024-09-14 05:23:01
实现一个函数来检查是否平衡树。这个问题而言。 平衡指的是这棵树随意两个叶子结点到根结点的距离之差不大于1。
这个题我们能够採用暴力搜索,找到叶子节点到根节点的最小值和最大值。然后他们的差假设大于1就不是平衡树,反之
则是平衡树。
int MinDepth = std::numeric_limits<int>::max();
int MaxDepth = std::numeric_limits<int>::min(); struct TreeNode
{
int data;
TreeNode* child;
TreeNode* brother;
}; bool isBlanceTree(const TreeNode* vNode, int vDepth=0)
{
if (vNode == NULL)
{
if (vDepth == 0) return true; if (vDepth < MinDepth)
{
MinDepth = vDepth;
} if (vDepth > MaxDepth)
{
MaxDepth = vDepth;
} if (MaxDepth - MinDepth > 1) return false;
return true;
} bool IsBlance = true;
if (vNode->child != NULL)
{
IsBlance = isBlanceTree(vNode->child, vDepth+1);
} if (IsBlance && vNode->brother != NULL)
{
IsBlance = isBlanceTree(vNode->brother, vDepth);
} return IsBlance;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
最新文章
- Excel—身份证生日提取
- discuz手机版模板开发
- Git平台使用时的配置分析
- Django MVC simple
- iOS:runtime最全的知识总结
- MySQL 单表百万数据记录分页性能优化
- css写法效率问题
- mybatis02 架构
- JavaScript XML 兼容处理,序列化和反序列化以及回调事件
- python entry points 例子
- jsonp总结
- 0Raspi开启root权限并登录使用
- javaweb学习总结(七)——HttpServletResponse对象(一)(转)
- Python3玩转儿 机器学习(2)
- PhoenixFD插件流体模拟——UI布局【Foam】详解
- K XOR Clique
- SNF快速开发平台MVC-各种级联绑定方式,演示样例程序(包含表单和表格控件)
- 【NOIP 2017】Day2 T3 列队
- kali 安装使用 sslocal
- emacs之配置之初始目录设置
热门文章
- css3-11 如何实现2D动画
- php实现合并两个排序的链表(很多情况下新建数组装东西比连东西逻辑快很多)($cur=$cur->;next;的理解)
- (十)RabbitMQ消息队列-高可用集群部署实战
- ios开发网络学习五:MiMEType ,多线程下载文件思路,文件的压缩和解压缩
- php实现 字符串加密(分类分布分工,化不可能为可能)
- [RxJS] Flatten a higher order observable with concatAll in RxJS
- java判断字符串是否为数字
- Codeforces 491B. New York Hotel 最远曼哈顿距离
- FullPage.js全屏滚动插件解说
- 前端css实现最基本的时间轴