LeetCode 110 Balanced Binary Tree

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

For this problem, a height-balanced binary tree is defined as:
a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
题意:
判断一颗二叉树是否是平衡二叉树,平衡二叉树的定义为,每个节点的左右子树深度相差小于1.

Example 1:

Given the following tree [3,9,20,null,null,15,7]:

    3/ \9  20/  \15   7

Return true.

Example 2:

Given the following tree [1,2,2,3,3,null,null,4,4]:

       1/ \2   2/ \3   3/ \4   4

Return false.

Solution 1:
这是和求最大深度的结合在一起,可以考虑写个helper函数找到拿到左右子树的深度,然后递归调用isBalanced函数判断左右子树是否也是平衡的,得到最终的结果。时间复杂度O(n^2)

    public boolean isBalanced(TreeNode root) {if (root == null) {return true;}int leftDep = depthHelper(root.left);int rightDep = depthHelper(root.right);if (Math.abs(leftDep - rightDep) <= 1 && isBalanced(root.left) && isBalanced(root.right)) {return true;}return false;}private int depthHelper(TreeNode root) {if (root == null) {return 0;}return Math.max(depthHelper(root.left), depthHelper(root.right)) + 1;}

Solution 2:
解题思路:
再来看个O(n)的递归解法,相比上面的方法要更巧妙。二叉树的深度如果左右相差大于1,则我们在递归的helper函数中直接return -1,那么我们在递归的过程中得到左子树的深度,如果=-1,就说明二叉树不平衡,也得到右子树的深度,如果=-1,也说明不平衡,如果左右子树之差大于1,返回-1,如果都是valid,则每层都以最深的子树深度+1返回深度。

    public boolean isBalanced(TreeNode root) {return dfsHeight(root) != -1;}//helper function, get the heightpublic int dfsHeight(TreeNode root){if (root == null) {return 0;}int leftHeight = dfsHeight(root.left);if (leftHeight == -1) {return -1;}int rightHeight = dfsHeight(root.right);if (rightHeight == -1) {return -1;}if (Math.abs(leftHeight - rightHeight) > 1) {return -1;}return Math.max(leftHeight, rightHeight) + 1;}

LeetCode 110 Balanced Binary Tree 平衡二叉树相关推荐

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

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

  2. LeetCode 110. Balanced Binary Tree

    LeetCode 110. Balanced Binary Tree 本博客参考自:http://www.cnblogs.com/grandyang/p/4045660.html Solution1: ...

  3. LeetCode 110 Balanced Binary Tree

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

  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. 110. Balanced Binary Tree 平衡二叉树

    给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. 示例 1: 给定二叉树 [3,9,20,null,nu ...

  7. [CareerCup] 4.1 Balanced Binary Tree 平衡二叉树

    4.1 Implement a function to check if a binary tree is balanced. For the purposes of this question, a ...

  8. 【leetcode】Balanced Binary Tree(middle)

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

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

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

最新文章

  1. Linux磁盘阵列技术详解(二)--raid 1创建
  2. 小米正用时序数据库,解决这个“硬核”问题
  3. box-cox数据规整转换
  4. java动漫网站开题报告_基于java的校园论坛网站的开发与设计开题报告.doc
  5. 计算机虚拟建造创新实践英语,《信息技术促进初中英语学科教学的实践研究》结题报告...
  6. 自监督学习和对比学习
  7. 未来:万物皆互联、全民皆社交,人人都是马化腾
  8. linux 5.5安装万兆网卡驱动,RedHat 5.5系统下安装MW54U无线USB网卡驱动
  9. cocos2d 屏幕適配_Cocos2d-x 3.1 一步步做屏幕适配
  10. Java垃圾回收日志解析
  11. 高仿114la网址导航源码完整最新版
  12. 6.java中什么是类_类、对象(java基础知识六)
  13. 《软件设计精要与模式》之Factory Method模式
  14. oracle erase,c++ stl容器vector删除(erase),遍历等基本用法介绍及头文件
  15. #openssl #爆重大漏洞heartbleed,危及两亿网民!!!
  16. Hyper-v 2.0
  17. 【元胞自动机】基于元胞自动机实现交通流NaSch模型matlab源码
  18. 基础学习——MySQL基础
  19. android 生成bks_android双向认证(自签名证书) bks的生成方法
  20. 第十二周项目四----利用遍历思想求解图问题之输出简单路径

热门文章

  1. ransac剔除误匹配matlab代码,基于APAP图像拼接算法的改进
  2. 怎么把自己建的墙拆掉_新房阳台栏杆要不要拆掉?后悔我家装修太早!
  3. 线性表算法题库_数据结构与算法(线性表)练习题
  4. eslint git提交不上_Git常用命令及日常问题集锦
  5. 判断一个对象是否存在某个键_面向工业大数据的对象存储技术实践
  6. linux 权限中加号,关于Linux中权限列中的加号及点的深度探索
  7. ant design pro 加载慢_ant design pro项目打包后页面加载缓慢
  8. 这便是有三AI一年的底蕴,那些5000粉丝1000阅读量的AI技术干货
  9. 《面向对象程序设计》第11章在线测试
  10. 《计算机组成原理》第05章在线测试