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。

第一种方式:递归算出每个节点的左右子树的高度,如果相差超过1,直接返回false。

第二种方式:DFS遍历一遍,遇到左右子树高度差超过1的,记录一个全局变量isBlance为false,最后返回这个boolean的全局变量即可。

显然,第二种方式更优,只需要从根节点计算一遍即可,而第一种方式有很多重复计算。

Talk is cheap>>

第一种方式:

public class BalancedBinaryTree {
public boolean isBalanced(TreeNode root) {
if (root == null)
return true;
List<TreeNode> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
TreeNode node = queue.get(0);
queue.remove(0);
if (Math.abs(maxDepth(node.left) - maxDepth(node.right)) > 1)
return false;
if (node.left != null) {
queue.add(node.left);
}
if (node.right != null) {
queue.add(node.right);
}
}
return true;
} public int maxDepth(TreeNode root) {
if (root == null)
return 0;
int l = maxDepth(root.left);
int r = maxDepth(root.right);
return 1 + Math.max(l, r);
}
}

第二种方式:

  private boolean result = true;

    public boolean isBalanced(TreeNode root) {
maxDepth(root);
return result;
} public int maxDepth(TreeNode root) {
if (root == null)
return 0;
int l = maxDepth(root.left);
int r = maxDepth(root.right);
if (Math.abs(l - r) > 1)
result = false;
return 1 + Math.max(l, r);
}

Balanced Binary Tree——LeetCode的更多相关文章

  1. 110.Balanced Binary Tree Leetcode解题笔记

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

  2. 110. Balanced Binary Tree - LeetCode

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

  3. Balanced Binary Tree [LeetCode]

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

  4. Balanced Binary Tree leetcode java

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

  5. Balanced Binary Tree --Leetcode C++

    递归 左子树是否为平衡二叉树 右子树是否为平衡二叉树 左右子树高度差是否大于1,大于1,返回false 否则判断左右子树 最简单的理解方法就是如下的思路: class Solution { publi ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

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

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

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

  8. LeetCode: Balanced Binary Tree 解题报告

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

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

    110. 平衡二叉树 110. Balanced Binary Tree 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点的左右两个子树 ...

随机推荐

  1. QT unit test code coverage

    准备环境: qt-creator5.2.1 , gcov(gcc 默认安装),lcov(gcov 的图形化显示界面),qt_testlib 各环境介绍: 1.gcov   gcov 是一个可用于C/C ...

  2. Camera2Raw

    This sample demonstrates how to use the Camera2 API to capture RAW camera buffers and save them as D ...

  3. C#中的操作数据库的SQLHelper类

    using System; using System.Collections.Generic; using System.Configuration; using System.Data; using ...

  4. datazen logo修改

    第一步:进入cp 页面 第二步: P161说明文档的P161开始,这里有说有版本的图片命名 第三步:再次强调第二步的命名,否则 如果你直接复制这个名字,不但效果出不来,还删除不掉这个包,我上次是把da ...

  5. SQL 中的好习惯和坏习惯

    在程序员日常的工作中,SQL可以说不可避免的,高效的SQL可以带来更加愉悦的体验.好的SQL书写习惯会给我们的工作带来极大的好处.简单总结下SQL的好习惯和坏习惯. IN和NOT IN 操作符 编码中 ...

  6. PHP MySQL 创建数据库

    PHP MySQL 创建数据库 数据库存有一个或多个表. 你需要 CREATE 权限来创建或删除 MySQL 数据库. 使用 MySQLi 和 PDO 创建 MySQL 数据库 CREATE DATA ...

  7. Hive学习之二 《Hive的安装之自定义mysql数据库》

    由于MySQL便于管理,在学习过程中,我选择MySQL. 一,配置元数据库. 1.安装MySQL,采用yum方式. ①yum  install  mysql-server,安装mysql服务端,安装服 ...

  8. pugixml使用教程

    pugixml介绍 pugixml是一个高性能.轻量级并且简单易用的xml解析库,支持UTF8 encoding.Little-endian UTF16.Big-endian UTF16.UTF16 ...

  9. ubuntu 更新 php5.5.9 到 php 5.6

    add-apt-repository ppa:ondrej/php5-5.6 apt-get update apt-get install php5 为了使用 add-apt-repsitory 需要 ...

  10. jQuery学习之过滤选择器

    基本过滤选择器 :first 选取第一个元素:$("div:first") :last 选取最后一个元素:$("div:last") :not(selector ...