算法:平衡二叉树110. Balanced Binary Tree
LeetCode全集请参考:LeetCode Github 大全
题目
- 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相关推荐
- LeetCode 110 Balanced Binary Tree 平衡二叉树
LeetCode 110 Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this ...
- leetCode 110. Balanced Binary Tree 平衡二叉树
110. Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...
- LeetCode 110. Balanced Binary Tree
LeetCode 110. Balanced Binary Tree 本博客参考自:http://www.cnblogs.com/grandyang/p/4045660.html Solution1: ...
- 平衡二叉树(Balanced Binary Tree)
平衡二叉树(Balanced Binary Tree) 平衡二叉树(Balanced Binary Tree)又称AVL树.AVL 树得名于它的发明者 G. M. Adelson-Velsky 和 E ...
- LeetCode 110 Balanced Binary Tree
LeetCode 110 Balanced Binary Tree Problem Description: 判断二叉树是不是平衡二叉树.所谓平衡二叉树,即每个节点的两个子树深度差的绝对值不超过1. ...
- LeetCode 110 Balanced Binary Tree(平衡二叉树)(*)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/5055 ...
- 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 ...
- 110. Balanced Binary Tree
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- leetcode python3 简单题110. Balanced Binary Tree
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百一十题 (1)题目 英文: Given a binary tree, dete ...
- 110. Balanced Binary Tree 平衡二叉树
给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. 示例 1: 给定二叉树 [3,9,20,null,nu ...
最新文章
- 揭秘PHP深受Web开发者喜爱的原因
- python读写文件函数_Python读写文件
- mysql7 zip windows_mysql 5.7 windows zip 版本安装配置
- fwink函数c语言,wink: Wink is oscstudio library
- 整人之bat重启文件
- springboot接入cas单点登录后跳转不到我需要跳转到页面_单点认证的一点心得
- 没推送功能,你好意思叫APP嘛?
- Oracle Compile 编译 无效对象(ORA-04063: package body SYS.DBMS_XPLAN 有错误)
- Matlab 实现对 Excel sheet 重命名 合并单元格
- MySQL JSON 类型数据操作
- php下列列表如何赋值,php学习笔记之list()赋值问题及each()结合遍历
- ubuntu 12.04安装vncserver
- cmake之TEST与TEST_F用法(五)
- php网页找注入点,另类找注入点技巧_91Ri.org
- 东方六爻周易排盘起卦软件,有电脑和手机Android App,Pad平板应用,卦例本机网络双备份,支持藏山卜
- 如何用Docker搭建自己的LANP|LNMP环境?
- G2O和Sliding Window中的Marginalization
- WINCC软件与西门子PLC以太网通讯
- 虚拟桌面的服务器,虚拟桌面与集合
- linux wenj 立即生效_linux方面知识
热门文章
- RBAC权限认证流程图
- mapper层中的SQLxml约束,头部标签
- linux fdisk ntfs,2014.1.2 学习记录(fdisk、ntfs)
- 全局变量在多个进程中不共享
- Android Studio 3.3发布:官方支持导航编辑器
- vmware中修改虚拟机MAC地址的方法!
- mac下安装wxPython2.8.12.1方法
- Error occurred during initialization of VM Could not reserve enough space for object heap
- oracle参数文件spfile和pfile
- jQuery 2.0.3 源码分析Sizzle引擎 - 编译函数(大篇幅)