Java for LeetCode 110 Balanced Binary Tree
2024-08-30 03:20:25
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.
解题思路:
递归即可,JAVA实现如下:
public boolean isBalanced(TreeNode root) {
if(root==null)
return true;
if(Math.abs(maxDepth(root.left)-maxDepth(root.right))>1)
return false;
return isBalanced(root.left)&&isBalanced(root.right);
}
static public int maxDepth(TreeNode root) {
if(root==null)
return 0;
return Math.max(maxDepth(root.left), maxDepth(root.right))+1;
}
最新文章
- GC
- YUM仓库服务
- linux中mysql如何设置为开机启动
- JAVA 多线程和并发学习笔记(四)
- LSD-SLAM深入学习(4)-非ROS改造
- 测试文档锁:doc.LockDocument()
- 【SpringMVC】SpringMVC系列9之Model数据返回到View
- CentOS7+Redis Live安装配置
- Android使用MVP时应该注意的问题
- 有关IT的小笑话
- [Swust 549]--变位词(vector水过)
- Swift开发学习(两):Playground
- wireshark抓包图解 TCP三次握手/四次挥手详解[转]
- bzoj 5297: [Cqoi2018]社交网络
- hive动态分区和混合分区
- webpack2入门概念
- 原创《weex面向未来的架构》
- Azure DevOps Server:Git权限设置
- C#内存映射文件消息队列实战演练(MMF—MQ)
- ASP.NET :Virtual Application vs Virtual Directory