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标记

直接码代码

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

  一次过了

看看 detail

2ms  感觉有点长了 我觉得应该是0ms级别的

惯例  学习别人的优秀解法  看看discuss 里最受欢迎的那个

public class Solution {
public boolean isBalanced(TreeNode root) {
if(null == root) {
return true;
} return Math.abs(height(root.left) - height(root.right)) < 2 && isBalanced(root.left) && isBalanced(root.right);
} public int height(TreeNode root) {
if(null == root) {
return 0;
} return 1 + Math.max(height(root.left), height(root.right));
}
}

 恩  其实和我的思路一样 只是写法不同  ~

110.Balanced Binary Tree Leetcode解题笔记的更多相关文章

  1. 110. Balanced Binary Tree - LeetCode

    Question 110. Balanced Binary Tree Solution 题目大意:判断一个二叉树是不是平衡二叉树 思路:定义个boolean来记录每个子节点是否平衡 Java实现: p ...

  2. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

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

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

  4. [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)

    Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...

  5. [LeetCode] 110. Balanced Binary Tree 解题思路

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

  6. 【一天一道LeetCode】#110. Balanced Binary Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  7. [LeetCode] 110. Balanced Binary Tree 平衡二叉树

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

  8. Leetcode 110 Balanced Binary Tree 二叉树

    判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...

  9. [LeetCode]题解(python):110 Balanced Binary Tree

    题目来源 https://leetcode.com/problems/balanced-binary-tree/ Given a binary tree, determine if it is hei ...

随机推荐

  1. NRF51822之IIC(MEMS_LIS2DH12)

    在上篇介绍了OLED的II以写操作为主,没有进行读取操作.所以在现再补充读取的操作. 我在此以LIS2DH为例子 uint8_t temp; lis2dh_read_registers(LIS2DH_ ...

  2. win10 剪贴板 拒绝访问 Cannot open clipboard

    win10 Cannot open clipboard:拒绝访问. 在RAD IDE代码编辑器中,双击选中的文本,会自动复制到剪贴板里,导致的问题是 从 A处复制文本 到B处双击选中,粘贴的时候,是B ...

  3. TortoiseGit 图标不显示

    1. 确认注册表:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\ShellIconOverlayIdent ...

  4. maven安装配置

    1.到官网下载maven http://maven.apache.org/download.html 2.解压后解压到任意文件路径 本地解压的位置:C:\soft\apache-maven-3.3.9 ...

  5. HP_UX系统批量创建LV或raw设备的Shell 脚本

    mkdir /dev/yjfsvg02 #创建lvm v2.0的VG,PE Size=64MB,不需要执行mknod创建group文件了.vgcreate -V 2.0 -s 64 -S 10t /d ...

  6. extjs ajax java简单精美验证码实现 有图

    前端:利用ExtJs的autoEl功能加载图片. var imgCheckValid = new Ext.create('Ext.Component',{ width: 70, //图片宽度 heig ...

  7. YII2学习第一天

    YII2学习第一天,之前稍微看了看TP,感觉和自己的理念不是很符合,然后转学YII2了. 使用的文档是https://github.com/yiisoft/yii2/tree/master/docs/ ...

  8. LeetCode 214 Shortest Palindrome

    214-Shortest Palindrome Given a string S, you are allowed to convert it to a palindrome by adding ch ...

  9. AWT事件处理

    AWT事件处理基本概念 AWT事件处理过程中,主要涉及3类对象: ①   Event(事件):用户对组件的一个操作,称之为一个事件,以类的形式出现,例如,键盘操作对应的事件类是KeyEvent.其实例 ...

  10. SQL语句中=null和is null

    平时经常会遇到这两种写法:IS NOT NULL与!=NULL.也经常会遇到数据库有符合条件!=NULL的数据,但是返回为空集合.实际上,是由于对二者使用区别理解不透彻. 默认情况下,推荐使用 IS ...