【题目】

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。那么这棵树就是平衡二叉树。

【比較直观的Java代码】

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

【改进后】

/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isBalanced(TreeNode root) {
height(root);
return run(root);
} public boolean run(TreeNode root) {
if (root == null) return true; int l = 0, r = 0;
if (root.left != null) l = root.left.val;
if (root.right != null) r = root.right.val;
if (Math.abs(l - r) <= 1 && isBalanced(root.left) && isBalanced(root.right)) return true; return false;
} public int height(TreeNode root) {
if (root == null) return 0;
root.val = Math.max( height(root.left), height(root.right) ) + 1;
return root.val;
}
}

利用了TreeNode结构中的val,用它来记录以当前结点为根的子树的高度,避免多次计算。

【LeetCode】Balanced Binary Tree 解题报告的更多相关文章

  1. LeetCode: Balanced Binary Tree 解题报告

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

  2. 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)

    [LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

  3. 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)

    [LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...

  4. 【LeetCode】331. Verify Preorder Serialization of a Binary Tree 解题报告(Python)

    [LeetCode]331. Verify Preorder Serialization of a Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...

  5. 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)

    [LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...

  6. 【LeetCode】236. Lowest Common Ancestor of a Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. 【LeetCode】1161. Maximum Level Sum of a Binary Tree 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...

  8. 【LeetCode】111. Minimum Depth of Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...

  9. 【LeetCode】104. Maximum Depth of Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:BFS 方法二:DFS 参考资料 日期 题目 ...

随机推荐

  1. hdu 2669(扩展欧几里得)

    Romantic Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  2. JS-严格模式、非严格模式

    2018年11月14日晚上,我在“深入理解javascript”书上第一次知道“严格模式”“非严格模式”这2个名词: “严格模式”使用指令:“use strict”: 这个指令我其实有经常看到,在其他 ...

  3. Java IO 学习(三)缓冲IO / 直接IO / 内存映射

    缓冲IO 在介绍缓冲IO之前需要先了解一下常用的机械硬盘的原理与特点 一个机械硬盘中装有多个盘片 每个盘片上有多个同心圆(磁道) 每个同心圆又由多个弧(扇区)组成,每个弧上都记录了等量的数据(比方说5 ...

  4. Cryptography I 学习笔记 --- 基于陷门置换的公钥加密

    RSA算法的工作流程 1. 生成公钥私钥 生成两个素数p和q,计算n=p*q,计算φ(n)=n-p-q+1,然后生成e与d,使 e * d = 1 mod φ(n). 然后以(n, e)作为公钥,(n ...

  5. JMeter 中Random 随机函数的使用

    场景:在做接口测试时,比如说要求用户的手机号码不允许重复,那此时可以通过Random 随机函数来解决此问题: 1.在JMeter 工具中,选择{选项-函数助手对话框-} 函数助手中选择 Random ...

  6. GRDB自定义的纯函数

    GRDB自定义的纯函数   在GRDB中,用户可以自定义SQlite函数.这样,在SQL语句中,可以直接调用这些函数.但是在定义的时候,用户需要指定函数的pure属性,表示该函数是否为纯函数.纯函数是 ...

  7. oracle中的替换函数replace和translate函数

    .translate 语法:TRANSLATE(char, from, to) 用法:返回将出现在from中的每个字符替换为to中的相应字符以后的字符串. 若from比to字符串长,那么在from中比 ...

  8. Maven项目导入到Eclipse时Build出现the user operation is waiting for building workspace to complete的问题解决

    解决办法如下: 1.选择菜单栏的[Project],然后把菜单栏中[Build Automatically]前面的对钩去掉.

  9. ARM Linux系统调用的原理

    转载自:http://blog.csdn.net/hongjiujing/article/details/6831192 ARM Linux系统调用的原理 操作系统为在用户态运行的进程与硬件设备进行交 ...

  10. DotnetBrowser高级教程-(4)使用MVC框架2-接收与返回模型

    在上一节,我们搭建了基本的mvc框架,这一节,我们将实现数据的接受与返回,具体操作如下: 1.新建Model目录,新增模型类Person.cs,代码如下: public class Person { ...