LeetCode 110. Balanced Binary Tree

本博客参考自:http://www.cnblogs.com/grandyang/p/4045660.html
Solution1:双递归解决问题!
高度平衡二叉树是每一个节点的两个字数的深度差不能超过1,那么我们肯定需要一个求各个点深度的函数,然后对每个节点的两个子树来比较深度差,时间复杂度为O(NlgN),代码如下:

class Solution {
public:bool isBalanced(TreeNode *root) {if (!root) return true;if (abs(getDepth(root->left) - getDepth(root->right)) > 1) return false;return isBalanced(root->left) && isBalanced(root->right);    }int getDepth(TreeNode *root) {if (!root) return 0;return 1 + max(getDepth(root->left), getDepth(root->right));}
};

Solution2:
上面那个方法正确但不是很高效,因为每一个点都会被上面的点计算深度时访问一次,我们可以进行优化。方法是如果我们发现子树不平衡,则不计算具体的深度,而是直接返回-1。那么优化后的方法为:对于每一个节点,我们通过checkDepth方法递归获得左右子树的深度,如果子树是平衡的,则返回真实的深度,若不平衡,直接返回-1,此方法时间复杂度O(N),空间复杂度O(H),参见代码如下:

class Solution {
public:    bool isBalanced(TreeNode *root) {if (checkDepth(root) == -1) return false;else return true;}int checkDepth(TreeNode *root) {if (!root) return 0;int left = checkDepth(root->left);if (left == -1) return -1;int right = checkDepth(root->right);if (right == -1) return -1;int diff = abs(left - right);if (diff > 1) return -1;else return 1 + max(left, right);}
};

LeetCode 110. Balanced Binary Tree相关推荐

  1. LeetCode 110 Balanced Binary Tree 平衡二叉树

    LeetCode 110 Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this ...

  2. LeetCode 110 Balanced Binary Tree

    LeetCode 110 Balanced Binary Tree Problem Description: 判断二叉树是不是平衡二叉树.所谓平衡二叉树,即每个节点的两个子树深度差的绝对值不超过1. ...

  3. leetCode 110. Balanced Binary Tree 平衡二叉树

    110. Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...

  4. LeetCode 110 Balanced Binary Tree(平衡二叉树)(*)

    版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/5055 ...

  5. C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解

    剑指offer 面试题39:判断平衡二叉树 提交网址:  http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...

  6. 【leetcode】Balanced Binary Tree(middle)

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  7. leetcode python3 简单题110. Balanced Binary Tree

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百一十题 (1)题目 英文: Given a binary tree, dete ...

  8. 110. Balanced Binary Tree

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  9. Leet Code OJ 110. Balanced Binary Tree [Difficulty: Easy]

    题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...

最新文章

  1. logging.Formatter 日期格式
  2. static的应用和作用
  3. ALBERT第一作者亲自讲解:Transformer、BERT、ALBERT、XLNet全面解析
  4. java中volatile关键字
  5. postgresql中uuid的使用
  6. java邮箱找回密码_java实现邮箱找回密码 简单邮件
  7. 第T题 详解放苹果(递归) =========== 把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同一种分法。
  8. iOS 评论中含有表情的处理方法
  9. 2016级算法第四次上机-A.Bamboo 和人工zz
  10. QGIS3.20 制作栅格动画
  11. python出现syntaxerror_Python SyntaxError语法错误原因及解决
  12. 工业控制系统如何实现网络安全等级保护的相关要求
  13. Druid 索引服务的资源精细化调度
  14. 深度学习数据集的准备
  15. Elastic 极客时间 阮一鸣 学习笔记_入门
  16. P5017 NOIP2018 普及组 摆渡车
  17. 江西单招计算机专业大学排名,2018年江西高职单招院校名单有哪些
  18. 分布式系统的8个谬误
  19. 音频界先驱杜比去世 享年80岁
  20. 安装Windows7虚拟机+ERPU8-10.1软件+SQLserver2008R2

热门文章

  1. 一些在PHPStudy部署中出现的问题解决
  2. VScode编辑同步Markdown文档到印象笔记
  3. python解码和编码的区别_python基础小知识,is和==的区别,编码和解码
  4. 理光m2554进入维修_理光DX2432C,基士得耶6201供墨检测代码,看完马上解决代码故障...
  5. Oracle数据库中dml提交,奇怪,ORACLE的触发器的DML操作,没有COMMIT,居然也能真正的提交掉???...
  6. 全国计算机等级考试报名入口黑龙江,黑龙江2021年3月计算机等级考试报名入口...
  7. 芜湖机器人餐厅地址_自助餐哪家强?得看谁让你扶墙... 芜湖8家自助餐厅超强攻略来了...
  8. java对象 引用 原理,java对象引用和对象值得行为
  9. 最大堆和最小堆和平衡二叉树_最小堆二叉树
  10. 创建队列 c语言_在C中创建队列