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

本题中,一棵高度平衡二叉树定义为:

一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1 。

示例 1:

输入:root = [3,9,20,null,null,15,7]

输出:true

示例 2:

输入:root = [1,2,2,3,3,null,null,4,4]

输出:false

示例 3:

输入:root = []

输出:true

解题思路

  1. 检查左子树的高度
  2. 检查右子树的高度
  3. 判断左右子树的高度差是否小于等于1
  4. 如果是,利用递归思想,继续检查左子节点和右子节点是否满足上述条件
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isBalanced(TreeNode root) {
if (root == null) {
return true;
} int leftDepth = depth(root.left);
int rightDepth = depth(root.right);
int distance = leftDepth > rightDepth ? leftDepth - rightDepth : rightDepth - leftDepth;
if (distance <= 1) {
return isBalanced(root.left) && isBalanced(root.right);
} else {
return false;
}
} private int depth(TreeNode node) {
int tdepth = 0; if (node == null) {
return tdepth;
} tdepth += 1;
int leftDepth = depth(node.left);
int rightDepth = depth(node.right);
int max = leftDepth > rightDepth ? leftDepth : rightDepth;
return max+tdepth;
}
}

leetcode 平衡二叉树的更多相关文章

  1. 二叉树的N中遍历方式和拓展应用

    (一)创建二叉树,如下图所示,一个标准的二叉树是所有位于根节点的左侧的子节点都是比根节点小的,右侧则都是大于根节点的. public class BinaryNode { public int val ...

  2. [LeetCode] Balanced Binary Tree 平衡二叉树

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

  3. LeetCode——Balanced Binary Tree(判断是否平衡二叉树)

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

  4. LeetCode之Balanced Binary Tree 平衡二叉树

    判定一棵二叉树是不是二叉平衡树. 链接:https://oj.leetcode.com/problems/balanced-binary-tree/ 题目描述: Given a binary tree ...

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

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

  6. [Leetcode] Balanced binary tree平衡二叉树

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

  7. LeetCode:平衡二叉树【110】

    LeetCode:平衡二叉树[110] 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. 示例 ...

  8. 【LeetCode】Balanced Binary Tree(平衡二叉树)

    这道题是LeetCode里的第110道题. 题目要求: 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. ...

  9. [LeetCode] 110. Balanced Binary Tree 平衡二叉树

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

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

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

随机推荐

  1. [转帖][minio]挂载minio到本地

    https://www.cnblogs.com/XY-Heruo/p/16489190.html 前言 将minio的bucket挂载到本地文件系统 环境 客户端系统版本:centos 7 MinIO ...

  2. [转帖]实战瓶颈定位-我的MySQL为什么压不上去–写场景

    https://plantegg.github.io/2023/06/30/%E5%AE%9E%E6%88%98%E7%93%B6%E9%A2%88%E5%AE%9A%E4%BD%8D-%E6%88% ...

  3. [转帖]Linux常用的一些命令,看你知道多少?

    https://zhuanlan.zhihu.com/p/115279009 Linux中命令有很多,而Linux系统中使用命令也是它的一大特点.在Linux系统中使用命令处理问题灵活,高效,所以熟知 ...

  4. Windows 可以操纵linux内文件,与本地一致的工具

    https://github.com/allanrbo/filesremote/releases/ 感觉挺好的.

  5. 对象数组,如果key中的value相同,不要该项

    <script type="text/javascript"> let arr=[ { gradeId: "498094709437239572", ...

  6. css 宽度分离原则

    我们想设计一个w=180px:h=100px的div; .demo1 { width: 180px; height: 100px; background: pink; padding: 10px; b ...

  7. 【笔记】学到几个 golang 代码小技巧

    作者:张富春(ahfuzhang),转载时请注明作者和引用链接,谢谢! cnblogs博客 zhihu Github 公众号:一本正经的瞎扯 从这篇文章学到:10个令人惊叹的Go语言技巧,让你的代码更 ...

  8. 开源IM项目OpenIM新版本发布-生产环境需更新

    项目简介 Android体验地址:https://www.pgyer.com/OpenIM 注册后自动加入组织,和群聊 群聊页面 工作台 工作台,业务可以通过jssdk自由扩展自身业务 工作圈 工作圈 ...

  9. C++ STL 标准模板库(排序/集合/适配器)算法

    C++ 标准模板库STL,是一个使用模板技术实现的通用程序库,该库由容器container,算法algorithm,迭代器iterator,容器和算法之间通过迭代器进行无缝连接,其中所包含的数据结构都 ...

  10. Nginx的反向代理做负载均衡

    对于一个大型网站,随着网站的访问量快速增长,单台服务器很难再支撑起需要,所以我们会购置多个服务器来满足业务量的需求,然后再利用Nginx提供的反向代理功能,来实现多台服务器间的协作功能,提高网站的处理 ...