问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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) {//传统递归法,简单易写//空树是平衡二叉树,因为左右子树数量都是 0if(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 刷题之路 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 ...

  3. LeetCode刷题记录6——696. Count Binary Substrings(easy)

    LeetCode刷题记录6--696. Count Binary Substrings(easy) 目录 LeetCode刷题记录6--696. Count Binary Substrings(eas ...

  4. LeetCode刷题记录4——67. Add Binary(easy)

    LeetCode刷题记录4--67. Add Binary(easy) 目录 LeetCode刷题记录4--67. Add Binary(easy) 题目 语言 思路 后记 题目 今天这题是与字符串相 ...

  5. LeetCode刷题(35)--Add Binary

    二进制加法: class Solution(object):def addBinary(self, a, b):""":type a: str:type b: str:r ...

  6. LeetCode 110 Balanced Binary Tree 平衡二叉树

    LeetCode 110 Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this ...

  7. leetCode 110. Balanced Binary Tree 平衡二叉树

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

  8. C#LeetCode刷题-树

    树篇 # 题名 刷题 通过率 难度 94 二叉树的中序遍历 61.6% 中等 95 不同的二叉搜索树 II 43.4% 中等 96 不同的二叉搜索树 51.6% 中等 98 验证二叉搜索树 22.2% ...

  9. 一个算法笨蛋的12月leetCode刷题日记

    类似文章 一个算法笨蛋的2021年11月leetCode刷题日记 一个算法笨蛋的2021年12月leetCode刷题日记 一个算法笨蛋的2022年1月leetCode刷题日记 一个算法笨蛋的2022年 ...

  10. 个人LeetCode刷题记录(带题目链接及解答)持续更新

    Leetcode 刷题 注:~[完成]代表还有一些方法没看,最后再看 一.一些需要重刷的典型题: 1.快速排序,归并排序,堆排序(递归的思想) 2.链表中的回文链表,其中的快慢指针,多看,多练 3.链 ...

最新文章

  1. javascript创建对象的三种方式
  2. AspxCallBack控件的CallBack事件
  3. kylin的安装和启动
  4. 关于CKEditor 4.0 过滤html标签
  5. springbatch读取外部数据到mysql
  6. python 如何安装软件包故障_Python安装软件包出错
  7. C++自学08:类型推断(auto/typeid)
  8. 东芝复印机2303出现f070_东芝复印机维修代码大全
  9. 2016 CSDN最佳博客(Android)
  10. python程序实现最大限度突破高德地图爬虫限制,包括.exe文件的编译,提供最大限度爬虫高德地图poi思路
  11. 一个很不错的H5动画网站
  12. 嵌入式linux IIO驱动
  13. 完整的rk3288调试记录
  14. 计算机论文折线图,干货 | 画论文折线图、曲线图?几个代码模板轻松搞定!
  15. 请原谅,我还来不及长大
  16. 机器人学--第二讲-齐次变换矩阵
  17. android悬浮按钮组件
  18. Linux常见问题汇总,比较适合菜鸟哈
  19. 计算机趣味数学社团活动管理制度,小学趣味数学社团活动管理制度
  20. 音频声学基础四,电声器件简介

热门文章

  1. MXNet.gluon——图像I/O
  2. 异常的分类 java 1615309080
  3. 分组框控件 1130
  4. 视图函数的基本理解 django
  5. dj鲜生-18-发送邮件功能
  6. laravel-admin form中的数据,在提交后,保存前,获取并进行编辑
  7. Nginx进程间通信机制
  8. HDU 6178 Monkeys
  9. 《技术的潜能:商业颠覆、创新与执行》一一2.12决心、愿望和耐力
  10. [.net 面向对象程序设计深入](36)Redis——基础