问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4074 访问。

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

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

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

给定二叉树 [3,9,20,null,null,15,7]

3

      / \

    9  20

   /       \

 15        7

返回 true 。

给定二叉树 [1,2,2,3,3,null,null,4,4]

1

      / \

     2   2

    / \

   3   3

  / \

 4   4

返回 false 。


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.

Given the following tree [3,9,20,null,null,15,7]:

3

      / \

    9  20

   /       \

15        7

Return true.

Given the following tree [1,2,2,3,3,null,null,4,4]:

1

      / \

     2   2

    / \

   3   3

  / \

 4   4

Return false.


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4074 访问。

public class Program {

    public static void Main(string[] args) {
var root = new TreeNode(1) {
left = new TreeNode(2),
right = new TreeNode(3)
}; var res = IsBalanced(root);
Console.WriteLine(res); root = new TreeNode(1) {
left = new TreeNode(2) {
left = new TreeNode(3) {
left = new TreeNode(4)
}
},
right = new TreeNode(5) {
right = new TreeNode(6)
}
}; res = IsBalanced2(root);
Console.WriteLine(res); Console.ReadKey();
} public static bool IsBalanced(TreeNode root) {
//传统递归法,简单易写
//空树是平衡二叉树,因为左右子树数量都是 0
if(root == null) return true;
//左右子树高度差大于 1,不是平衡二叉树
if(Math.Abs(MaxDepth(root.left) - MaxDepth(root.right)) > 1) return false;
//判定左右子树是否为平衡二叉树
return IsBalanced(root.left) && IsBalanced(root.right);
} public static int MaxDepth(TreeNode root) {
//计算树每个子树的高度
if(root == null) return 0;
var left = MaxDepth(root.left);
var right = MaxDepth(root.right);
return Math.Max(left, right) + 1;
} public static bool IsBalanced2(TreeNode root) {
//优化递归法
//此段代码引用自 LeetCode 的提交代码
//由本人添加注释
return MaxDepth2(root) >= 0;
} public static int MaxDepth2(TreeNode root) {
//如果是空树,判定为平衡的
if(root == null) return 0;
//计算左右子树的高度
var left = MaxDepth2(root.left);
var right = MaxDepth2(root.right);
//如果左子树、右子树或高度差大于 1,则返回 -1
//-1 表示判定为非平衡树,立即返回
//导致调用堆栈在回溯时发现上一次是 -1
//由于代码 left < 0 || right < 0 的存在,会使 -1 逐步上传
//又被回溯到上上一次,一直到无法回溯时为止
//按照原代码作者的部分注释,即不平衡的树会被传染到最上层
//也即当发现一个 -1 时,该代码以非常高的效率判定原树为非平衡的
//因为当前树为平衡的,不能判定原树是不是平衡的
//但当前树为非平衡的,原树肯定是不平衡的
//感谢原作者的代码为我们分享如此巧妙的优化
if(left < 0 || right < 0 || Math.Abs(left - right) > 1) return -1;
return Math.Max(left, right) + 1;
} public class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int x) { val = x; }
} }

以上给出2种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4074 访问。

True
False

分析:

显而易见,以上2种算法的时间复杂度均为:  。

C#LeetCode刷题之#110-平衡二叉树(Balanced Binary Tree)的更多相关文章

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

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

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

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

  3. leetcode 刷题之路 64 Construct Binary Tree from Inorder and Postorder Traversal

    Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...

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

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

  5. 平衡二叉树Balanced Binary Tree

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

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

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

  7. LeetCode算法题-Average of Levels in Binary Tree(Java实现)

    这是悦乐书的第277次更新,第293篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第145题(顺位题号是637).给定一个非空二叉树,以数组的形式返回每一层节点值之和的平 ...

  8. leetcode刷题-559. Maximum Depth of N-ary Tree

    题目: https://leetcode.com/problems/maximum-depth-of-n-ary-tree/description/ n-ary-tree的数据结果表示 // Defi ...

  9. [leetcode刷题笔记]Implement Trie (Prefix Tree)

    题目链接 一A,开森- ac代码: class TrieNode { // Initialize your data structure here. char content; boolean isW ...

  10. LeetCode(110) Balanced Binary Tree

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

随机推荐

  1. 在ShareX里添加流浪图床

    这里以咱流浪图床为例哈:-D 上传目标类型:图像.文件 请求方法:POST 请求URL:https://p.sda1.dev/api/v1/upload_external_noform URL参数:名 ...

  2. 【JVM之内存与垃圾回收篇】堆

    堆 堆的核心概念 堆针对一个 JVM 进程来说是唯一的,也就是一个进程只有一个 JVM,但是进程包含多个线程,他们是共享同一堆空间的. 一个 JVM 实例只存在一个堆内存,堆也是 Java 内存管理的 ...

  3. Python 脚本语言

    python 脚本语言(python的命名起源于一个脚本screenplay,每次运行都会使对话框逐字重复.由著名的“龟叔”Guido van Rossum在1989年圣诞节期间编写.) Python ...

  4. xctf-web fakebook

    点join注册账号 进入view.php发现参数no存在sql注入,但是过滤了select关键字,用内联注释绕过 在users表的data字段发现了用序列化存储的用户信息 然后就卡在这里了....看了 ...

  5. 如何导入spring 的jar包到eclips

    https://www.cnblogs.com/xxuan/p/6949640.html

  6. pandas之时间数据

    1.时间戳Timestamp() 参数可以为各种形式的时间,Timestamp()会将其转换为时间. time1 = pd.Timestamp('2019/7/13') time2 = pd.Time ...

  7. 人车识别实验丨华为ModelArts VS 百度Easy DL硬核体验

    摘要:想了解时下流行的自动驾驶相关AI模型吗?接下来就用华为云的ModelArts和百度的Easy DL带你体验一下AI平台是怎么进行模型训练的. 华为ModelArts自动学习 VS 百度Easy ...

  8. vuex多多,怎么当好一个奶妈

    前言 vue 本身更偏向于 view 层的框架,尤大大并没有一开始就给他一个完整的 mvvm 架构. 在 vue 的世界里 vuex 是用来实现 mvvm 中关键的 vm 层(视图模型层),你甚至可以 ...

  9. acwing 173. 矩阵距离(bfs)

    给定一个N行M列的01矩阵A,A[i][j] 与 A[k][l] 之间的曼哈顿距离定义为: dist(A[i][j],A[k][l])=|i−k|+|j−l|dist(A[i][j],A[k][l]) ...

  10. JPA第一天

    学于黑马和传智播客联合做的教学项目 感谢 黑马官网 传智播客官网 微信搜索"艺术行者",关注并回复关键词"springdata"获取视频和教程资料! b站在线视 ...