LeetCode全集请参考:LeetCode Github 大全

题目

  1. 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 left and right subtrees of every node differ in height by no more than 1.

Example 1:

Input: root = [3,9,20,null,null,15,7]
Output: true

Example 2:

Input: root = [1,2,2,3,3,null,null,4,4]
Output: false

Example 3:

Input: root = []
Output: true

Constraints:

The number of nodes in the tree is in the range [0, 5000].
-104 <= Node.val <= 104

深度优先解法

如果左右子树的深度差超过1则,直接退出。

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/
class Solution {public boolean isBalanced(TreeNode root) {// check edgeif(root == null) {return true;}return gainHeight(root) != -1;}private int gainHeight(TreeNode root) {if (root == null) {return 0;}int left = gainHeight(root.left);if (left == -1) {return -1;}int right = gainHeight(root.right);if (right == -1) {return -1;}if (Math.abs(left - right) > 1) {return -1;}return 1 + Math.max(left, right);}
}

算法:平衡二叉树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 平衡二叉树

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

  3. LeetCode 110. Balanced Binary Tree

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

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

    平衡二叉树(Balanced Binary Tree) 平衡二叉树(Balanced Binary Tree)又称AVL树.AVL 树得名于它的发明者 G. M. Adelson-Velsky 和 E ...

  5. LeetCode 110 Balanced Binary Tree

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

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

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

  7. 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 ...

  8. 110. Balanced Binary Tree

    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 ...

  10. 110. Balanced Binary Tree 平衡二叉树

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

最新文章

  1. 揭秘PHP深受Web开发者喜爱的原因
  2. python读写文件函数_Python读写文件
  3. mysql7 zip windows_mysql 5.7 windows zip 版本安装配置
  4. fwink函数c语言,wink: Wink is oscstudio library
  5. 整人之bat重启文件
  6. springboot接入cas单点登录后跳转不到我需要跳转到页面_单点认证的一点心得
  7. 没推送功能,你好意思叫APP嘛?
  8. Oracle Compile 编译 无效对象(ORA-04063: package body SYS.DBMS_XPLAN 有错误)
  9. Matlab 实现对 Excel sheet 重命名 合并单元格
  10. MySQL JSON 类型数据操作
  11. php下列列表如何赋值,php学习笔记之list()赋值问题及each()结合遍历
  12. ubuntu 12.04安装vncserver
  13. cmake之TEST与TEST_F用法(五)
  14. php网页找注入点,另类找注入点技巧_91Ri.org
  15. 东方六爻周易排盘起卦软件,有电脑和手机Android App,Pad平板应用,卦例本机网络双备份,支持藏山卜
  16. 如何用Docker搭建自己的LANP|LNMP环境?
  17. G2O和Sliding Window中的Marginalization
  18. WINCC软件与西门子PLC以太网通讯
  19. 虚拟桌面的服务器,虚拟桌面与集合
  20. linux wenj 立即生效_linux方面知识

热门文章

  1. RBAC权限认证流程图
  2. mapper层中的SQLxml约束,头部标签
  3. linux fdisk ntfs,2014.1.2 学习记录(fdisk、ntfs)
  4. 全局变量在多个进程中不共享
  5. Android Studio 3.3发布:官方支持导航编辑器
  6. vmware中修改虚拟机MAC地址的方法!
  7. mac下安装wxPython2.8.12.1方法
  8. Error occurred during initialization of VM Could not reserve enough space for object heap
  9. oracle参数文件spfile和pfile
  10. jQuery 2.0.3 源码分析Sizzle引擎 - 编译函数(大篇幅)