C#LeetCode刷题之#110-平衡二叉树(Balanced Binary Tree)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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 7Return true.
Given the following tree [1,2,2,3,3,null,null,4,4]:
1
/ \
2 2
/ \
3 3
/ \
4 4Return 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)的更多相关文章
- LeetCode 110. 平衡二叉树(Balanced Binary Tree) 15
110. 平衡二叉树 110. Balanced Binary Tree 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点的左右两个子树 ...
- [LeetCode 110] - 平衡二叉树 (Balanced Binary Tree)
问题 给出一棵二叉树,判断它是否在高度上是平衡的. 对于本问题,高度上平衡的二叉树定义为:每个节点的两棵子树的深度差永远不大于1的一棵二叉树. 初始思路 根据定义,思路应该比较直接:递归计算每个节点左 ...
- 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 ...
- 平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称AVL树
平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称AVL树 (a)和(b)都是排序二叉树,但是查找(b)的93节点就需要查找6次,查找(a)的93 ...
- 平衡二叉树Balanced Binary Tree
[抄题]: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced b ...
- [Swift]LeetCode110. 平衡二叉树 | Balanced Binary Tree
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- LeetCode算法题-Average of Levels in Binary Tree(Java实现)
这是悦乐书的第277次更新,第293篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第145题(顺位题号是637).给定一个非空二叉树,以数组的形式返回每一层节点值之和的平 ...
- leetcode刷题-559. Maximum Depth of N-ary Tree
题目: https://leetcode.com/problems/maximum-depth-of-n-ary-tree/description/ n-ary-tree的数据结果表示 // Defi ...
- [leetcode刷题笔记]Implement Trie (Prefix Tree)
题目链接 一A,开森- ac代码: class TrieNode { // Initialize your data structure here. char content; boolean isW ...
- LeetCode(110) Balanced Binary Tree
题目 Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bina ...
随机推荐
- mysql 利用延迟关联优化查询(select * from your_table order by id desc limit 2000000,20)
其实在我们的工作中类似,select * from your_table order by id desc limit 2000000,20会经常遇见,比如在分页中就很常见. 如果我们的sql中出现这 ...
- jupyter中%matplotlib inline报错
学习matplotlib时,使用的jupyter跑代码.报错如上图.大致就是后面的注释不能被识别.我寻思着注释不用识别吧,大概是因为%后跟的语句被全部当成命令行执行了,然后命令行不识别行内注释,导致报 ...
- k8s极简史:K8s多集群技术发展的历史、现状与未来
引子 随着云原生技术的普及,越来越多的企业使用Kubernetes来管理应用,并且集群规模也呈爆发式增长,企业也亟需应对随集群规模增长而带来的各种挑战.同时,为了更好地提供高可用.弹性伸缩的应用,企业 ...
- Monster Audio 使用教程 (八) Vst3 使用侧链功能
Monster Audio对 Vst3 插件支持侧链功能,例如,我们插入一个Waves C1 comp Stereo 效果器 然后在侧链处,就可以选择任意一个音轨的信号,作为侧链信号源. 注意,只有v ...
- 理解Spring(二):AOP 的概念与实现原理
目录 什么是 AOP AOP 的基本术语 Spring AOP 的简单应用 Spring AOP 与动态代理 Spring AOP 的实现原理(源码分析) 扩展:为什么 JDK 动态代理要求目标类必须 ...
- 数字麦克风PDM信号采集与STM32 I2S接口应用(四)--单片机源码
本文是数字麦克风笔记文章的单片机程序.一些朋友私信我,调试出问题. 我就把源码贴出来吧,可能主要问题是DMA的配置. 尤其双DMA时候,需要手动启动I2S的接收DMA,HAL库没有这个接口,不看dat ...
- 【接单】找我付费定制Python工具软件或网站开发、Chrome浏览器插件、油猴脚本
各位可付费找我定制Python工具软件或网站开发.Chrome插件.油猴脚本.自动化软件,可通过我做的软件来评判我的实力,一定要先和我沟通你的需求,做不了的我也不会接. 费用50元起,通过淘宝APP或 ...
- Python 图像处理 OpenCV (15):图像轮廓
前文传送门: 「Python 图像处理 OpenCV (1):入门」 「Python 图像处理 OpenCV (2):像素处理与 Numpy 操作以及 Matplotlib 显示图像」 「Python ...
- DVWA SQL 注入关卡初探
1. 判断回显 给id参数赋不同的值,发现有不同的返回信息 2. 判断参数类型 在参数后加 ' ,查看报错信息 数字型参数左右无引号,字符型参数左右有引号 4. 引号闭合与布尔类型判断 由于是字符型参 ...
- Django开发之Ajax POST提交403报错
问题现象 Django开发时,前端post提交数据时,由于csrf机制,如果不做处理会报403报错 问题解决 通过在data字段中添加 csrfmiddlewaretoken: '{{ csrf_to ...