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

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

一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 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. [转帖]docker容器自动重启,看完这篇彻底明白了

    一. JVM内存区域的划分 1.1  java虚拟机运行时数据区 java虚拟机运行时数据区分布图: JVM栈(Java Virtual Machine Stacks): Java中一个线程就会相应有 ...

  2. [转帖]比 Python 快 35000 倍!LLVM&Swift 之父宣布全新编程语言 Mojo:编程被颠覆了

    https://www.infoq.cn/article/GFfVLVpkIGOcKYB85Opb "Mojo 可能是近几十年来最大的编程语言进步." 近日,由 LLVM 和 Sw ...

  3. [转帖]013 Linux 搞懂「文件所属者更改及权限的赋予」从未如此简单 (chmod、chgrp、chown)

    https://my.oschina.net/u/3113381/blog/5435014   01 一图详解「ls -l」 02 两种符号区分表示文件和目录 -(横线) # 表示非目录文件 d # ...

  4. tomcat各版本与jdk及servlet各版本对应关系

    在项目部署的时候,如果对于Web应用没有选择正确的Web服务器版本,应用可能不能正常运行.下图为官方给的Servlet/JSP各规范与Web服务器Tomcat各版本的对应关系,如:支持Servlet ...

  5. gitee 命令合集(从远程仓库拉取项目到推送项目到远程仓库)

    1.配置用户的信息 git config --global user.name '你的用户名' git config --global user.email '你的邮箱' 2.初始化 Git 仓库,生 ...

  6. Web-background information

    Client and Server A connection on the Internet takes place between 2 computers only: one that has th ...

  7. Azure - 机器学习:创建机器学习所需资源,配置工作区

    本文中你可以创建使用 Azure 机器学习所需的资源,包含工作区和计算实例. 关注TechLead,分享AI全维度知识.作者拥有10+年互联网服务架构.AI产品研发经验.团队管理经验,同济本复旦硕,复 ...

  8. Excel-批量填充数字

    1.一般情况下,都是使用鼠标左右键拖动来实现数据的填充的 2.但是填充1200列,下拉拖动就非常麻烦,可以首先定位到A200. 在屏幕左侧中央处找到剪切板下方的"A1"字样,鼠标单 ...

  9. Linux 环境下使用 sqlplus 访问远程 Oracle 数据库

    自己最近需要在 Oracle 生产环境检查一些数据,但是目前大多数的生产环境,出于安全考虑,不会提供图形界面让你使用类似 Navicat 工具让你直接访问数据库,网上找了很多资料,大部分都比较过时或者 ...

  10. NC53079 Forsaken喜欢数论

    题目链接 题目 题目描述 ​ Forsaken有一个有趣的数论函数.对于任意一个数 \(x\) , \(f(x)\) 会返回 \(x\) 的最小质因子.如果这个数没有最小质因子,那么就返回0. ​ 现 ...