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.

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.

解决方案(完美版):

class Solution {
public boolean isBalanced(TreeNode root) {
return helper(root) != -1;
} public int helper(TreeNode root){
if(root == null) return 0; int left = helper(root.left);
int right = helper(root.right); if(left == -1 || right == -1 || Math.abs(left - right) > 1) return -1; return Math.max(left, right) + 1;
}
}

自创版本:

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

递归 - Leetcode 110 判断二叉树是否为平衡二叉树的更多相关文章

  1. 【剑指offer】判断二叉树是否为平衡二叉树

    2013-09-03 14:16:51 面试题39:求二叉树的深度.判断二叉树是否为平衡二叉树 小结: 根据平衡二叉树的定义,需要判断每个结点,因此,需要遍历二叉树的所有结点,并判断以当前结点为根的树 ...

  2. [Leetcode 100]判断二叉树相同 Same Tree

    [题目] 判断二叉树是否相同. [思路] check函数. p==null并且q==null,返回true;(两边完全匹配) p==null或q==null,返回false;(p.q其中一方更短) p ...

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

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

  4. leetcode 98,判断二叉树为BST

    方法一,记录子树的上界和下界,root的左子树一定小于root的值,root的右子树一定大于root的值,然后递归左子树和右子树 public class Solution { public bool ...

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

    翻译 给定一个二叉树,决定它是否是高度平衡的. (高度是名词不是形容词-- 对于这个问题.一个高度平衡二叉树被定义为: 这棵树的每一个节点的两个子树的深度差不能超过1. 原文 Given a bina ...

  6. 【二叉树的递归】03判断二叉树中有没有和为给定值的路径【Path Sum】

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树和一个和,判断这个树 ...

  7. leetcode 110

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

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

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

  9. 56. 2种方法判断二叉树是不是平衡二叉树[is balanced tree]

    [本文链接] http://www.cnblogs.com/hellogiser/p/is-balanced-tree.html [题目] 输入一棵二叉树的根结点,判断该树是不是平衡二叉树.如果某二叉 ...

随机推荐

  1. i春秋 百度杯”CTF比赛 十月场 login

    出现敏感的信息,然后进行登录 登录成功发现奇怪的show 然后把show放到发包里面试一下 出现了源码,审计代码开始 出flag的条件要user 等于春秋 然后进行login来源于反序列化后的logi ...

  2. Guava Cache探索及spring项目整合GuavaCache实例

    背景 对于高频访问但是低频更新的数据我们一般会做缓存,尤其是在并发量比较高的业务里,原始的手段我们可以使用HashMap或者ConcurrentHashMap来存储. 这样没什么毛病,但是会面临一个问 ...

  3. 04-JavaScript之常见运算符

    JavaScript之常见运算符 1.赋值运算符 以var x=12,y=5来演示示例 运算符 例子 等同于 运算结果 = x=y   x=5 += x+=y x=x+y x=17 -= x-=y x ...

  4. lombok的简单使用小结

    1.idea安装lombok插件 关于lombok如何在idea中使用,下面这篇博客写的很到位,并且提供了本地安装对应idea版本的lombok插件的地址.如果无法通过idea直接安装lombok,可 ...

  5. Android技术框架——Dagger2

    Dagger2 是一个Android依赖注入框架.没错依赖注入,学习过spring的同学看到这词,应该是挺熟悉的.当然既然是Android的课题,我们就来聊聊Dagger2 ,android开发当前非 ...

  6. JAVA并发包学习

    1)CyclicBarrier一个同步辅助类,允许一组线程相互等待,直到这组线程都到达某个公共屏障点.该barrier在释放等待线程后可以重用,因此称为循环的barrier 2)CountDownLa ...

  7. mpvue——支持less

    安装 安装less和less-loader,我用的是淘宝源,你也可以直接npm $ cnpm install less less-loader --save 配置 打开build目录下的webpack ...

  8. sqlite 数据库 boolean类型的小小测试

    根据官方文档的介绍: SQLite does not have a separate Boolean storage class. Instead, Boolean values are stored ...

  9. Java【初识篇】语言概述

    什么是计算机语言 语言:是人与人之间用于沟通的一种方式.例如:中国人与中国人用普通话沟通.而中国人要和英国人交流,就要学习英语.计算机语言(编程语言):人与计算机交流的方式.如果人要与计算机交流,那么 ...

  10. shell实战之日志备份

    util.sh #!/bin/bash - # Read config # kay # Version: 1.0 # // # configuration file path config=${log ...