实现一个函数来检查是否平衡树。这个问题而言。 平衡指的是这棵树随意两个叶子结点到根结点的距离之差不大于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;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

最新文章

  1. Excel—身份证生日提取
  2. discuz手机版模板开发
  3. Git平台使用时的配置分析
  4. Django MVC simple
  5. iOS:runtime最全的知识总结
  6. MySQL 单表百万数据记录分页性能优化
  7. css写法效率问题
  8. mybatis02 架构
  9. JavaScript XML 兼容处理,序列化和反序列化以及回调事件
  10. python entry points 例子
  11. jsonp总结
  12. 0Raspi开启root权限并登录使用
  13. javaweb学习总结(七)——HttpServletResponse对象(一)(转)
  14. Python3玩转儿 机器学习(2)
  15. PhoenixFD插件流体模拟——UI布局【Foam】详解
  16. K XOR Clique
  17. SNF快速开发平台MVC-各种级联绑定方式,演示样例程序(包含表单和表格控件)
  18. 【NOIP 2017】Day2 T3 列队
  19. kali 安装使用 sslocal
  20. emacs之配置之初始目录设置

热门文章

  1. css3-11 如何实现2D动画
  2. php实现合并两个排序的链表(很多情况下新建数组装东西比连东西逻辑快很多)($cur=$cur-&gt;next;的理解)
  3. (十)RabbitMQ消息队列-高可用集群部署实战
  4. ios开发网络学习五:MiMEType ,多线程下载文件思路,文件的压缩和解压缩
  5. php实现 字符串加密(分类分布分工,化不可能为可能)
  6. [RxJS] Flatten a higher order observable with concatAll in RxJS
  7. java判断字符串是否为数字
  8. Codeforces 491B. New York Hotel 最远曼哈顿距离
  9. FullPage.js全屏滚动插件解说
  10. 前端css实现最基本的时间轴