110. 平衡二叉树

110. Balanced Binary Tree

题目描述

给定一个二叉树,判断它是否是高度平衡的二叉树。

本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点的左右两个子树的高度差的绝对值不超过 1。

每日一算法2019/5/18Day 15LeetCode110. Balanced Binary Tree

示例 1:

给定二叉树 [3,9,20,null,null,15,7]
```
3
/ \
9 20
/ \
15 7
```
返回 true。

示例 2:

给定二叉树 [1,2,2,3,3,null,null,4,4]
```
1
/ \
2 2
/ \
3 3
/ \
4 4
```
返回 false。

Java 实现

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

相似题目

参考资料

LeetCode 110. 平衡二叉树(Balanced Binary Tree) 15的更多相关文章

  1. [LeetCode 110] - 平衡二叉树 (Balanced Binary Tree)

    问题 给出一棵二叉树,判断它是否在高度上是平衡的. 对于本问题,高度上平衡的二叉树定义为:每个节点的两棵子树的深度差永远不大于1的一棵二叉树. 初始思路 根据定义,思路应该比较直接:递归计算每个节点左 ...

  2. 平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称AVL树

    平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称AVL树 (a)和(b)都是排序二叉树,但是查找(b)的93节点就需要查找6次,查找(a)的93 ...

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

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

  4. LeetCode(110) Balanced Binary Tree

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

  5. [Swift]LeetCode110. 平衡二叉树 | Balanced Binary Tree

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

  6. 【LeetCode OJ】Balanced Binary Tree

    Problem Link: http://oj.leetcode.com/problems/balanced-binary-tree/ We use a recursive auxilar funct ...

  7. 平衡二叉树Balanced Binary Tree

    [抄题]: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced b ...

  8. LeetCode算法题-Balanced Binary Tree(Java实现)

    这是悦乐书的第167次更新,第169篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第26题(顺位题号是110).给定二叉树,判断它是否是高度平衡的.对于此问题,高度平衡二 ...

  9. LeetCode题解之Balanced Binary Tree

    1.题目描述 2.问题分析 DFS. 3.代码 bool isBalanced(TreeNode* root) { if (root == NULL) return true; && ...

随机推荐

  1. 关于连接sftp以及本地配置sftp的事情

    1.window下配置sftp服务器 参考:https://blog.csdn.net/zhangliang_571/article/details/45598939 下载:http://www.fr ...

  2. java如何实现多线程?线程的状态有哪些?

    java实现多线程有两种方法    1.继承Thread类    2.实现Runnable接口    这两种方法的共同点:    不论用哪种方法,都必须用Thread(如果是Thead子类就用它本身) ...

  3. Leetcode32. 最长有效括号

    32. 最长有效括号 做法 \(f_{i}\)以\(i\)结尾的最长匹配 前提为\(s[i]=')'\) \(s[i-1]='('\),则\(f[i]=f[i-2]+2\) \(s[i-1]=')'\ ...

  4. vue日常学习(2)

    1.组件学习之内容分发 1.1 作用域插槽 父级 <div class="parent"> <child> <template scope=" ...

  5. Linux 之数组

    数组 和其他编程语言一样,Shell 也支持数组.数组(Array)是若干数据的集合,其中的每一份数据都称为元素(Element). Shell 并且没有限制数组的大小,理论上可以存放无限量的数据.和 ...

  6. Pycon 北京2019

  7. php-浮点数计算,double类型数加减乘除必须用PHP提供的高精度计算函数

    一.前方有坑 php在使用加减乘除等运算符计算浮点数的时候,经常会出现意想不到的结果,特别是关于财务数据方面的计算,给不少工程师惹了很多的麻烦.比如今天工作终于到的一个案例: $a = 2586; $ ...

  8. ISO/IEC 9899:2011 条款6.7.2——类型说明符

    6.7.2 类型说明符 语法 1.type-specifier: void char short int long float double signed unsigned _Bool _Comple ...

  9. java 数据相除

    编程的人都知道,java中的“/”.“%”运算,其中前者为取整,后者取余数.那么有没有快捷的运算方法取正常的运算结果呢? /** * TODO 除法运算,保留小数 * @author 袁忠明 * @d ...

  10. php报错syntax error, unexpected T_GOTO, expecting T_STRING,报错文件与行数指向以下代码,是什么原因?

    本机php版本是5.3.8,Apache/2.2.21public function goto($url, $msg=NULL) {if ($msg) {$this->jsAlert($msg) ...