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. 6、httpd2.4 编译安装LAMP

    www.itjc8.com 新特性: MPM支持运营DSO机制(动态共享对象),以模块形式按需加载 支持event MPM 支持异步读写 支持每模块及每个目录分别使用各自的日志级别 每请求配置 增强版 ...

  2. Java 使用Jedis和RedisTemplate操作Redis缓存(SpringBoot)

    package com.example.redis.controller; import com.example.redis.entity.User; import com.example.redis ...

  3. ubuntu之路——day11.4 定位数据不匹配与人工合成数据

    1.人工检验train和dev/test之间的区别: 比如:汽车语音识别中的噪音.地名难以识别等等 2.使得你的训练集更靠近(相似于)dev/test,收集更多类似于dev的数据: 比如:dev中存在 ...

  4. 使用 concurrently 并行地运行多个命令(同时跑前端和后端的服务)

    我现在有一个项目是这样的,前端是用 React 写的,后端是用 Nodejs,目录结构如下: . ├── README.md ├── backend ├── node_modules ├── pack ...

  5. 【转】利用Python将多个PDF合并为一个

    本脚本用来合并pdf文件,输出的pdf文件按输入的pdf文件名生成书签 使用示例如下: python pdfmerge.py -p "D:\pdf-files" -o " ...

  6. OSG学习笔记0——解决OSG读obj模型问题[转]

    原文:https://blog.csdn.net/u011310341/article/details/51179948 #include "stdafx.h" #include& ...

  7. eQTL | Expression quantitative trait loci | 表达数量性状基因座 | QTL | 数量性状位点

    到底什么是eQTL? eQTL和QTL之间有什么联系?为什么说QTL比eQTL难很多? QTL和GWAS有什么关系? GTEx数据库里的eQTL数据如何利用? 说eQTL之前必须先解释QTL,QTL, ...

  8. Linux 限制IP——/etc/hosts.allow和/etc/hosts.deny文件【转】

    就像是 限制特定IP来访 想法 看起来通常的做法是利用hosts的拒绝设置,而它的设置是针对某一个具体的进程,具体的服务,在这里就是sshd了 看起来设置一个网段使用的是 x.x.x.0/24 后面加 ...

  9. Robot Framework安装使用

    关于robotframework环境搭建安装请参考 另外一篇博文:Robot Framework的环境搭建(就是一些库文件的安装) 项目基本流程: 1.创建项目New Project----选择dir ...

  10. gitlab 构建常见错误

    1.前端是http服务后端是https,原因生产https,测试是http服务环境.代理后端2. java 打包程序需要运行正式数据库没连上错误.打包和跑正式的一个库.3. jenkins不能直接no ...