当前位置:网站首页>[leetcode] 93 balanced binary tree
[leetcode] 93 balanced binary tree
2020-11-10 01:15:00 【JaneRoad】
subject :
Their thinking :
Bottom up recursion
Similar to post order traversal , For the node currently traversed , First, recursively judge whether the left and right subtrees are balanced , Then judge whether the subtree with the current node as the root is balanced . If a subtree is balanced , Then return to its height ( The height must be a nonnegative integer ), Otherwise return to -1−1. If there is a subtree imbalance , Then the whole binary tree must be unbalanced .
Code :
class Solution {
public boolean isBalanced(TreeNode root) {
return height(root) >= 0;
}
public int height(TreeNode root) {
if (root == null) {
return 0;
}
int leftHeight = height(root.left);
int rightHeight = height(root.right);
if (leftHeight == -1 || rightHeight == -1 || Math.abs(leftHeight - rightHeight) > 1) {
return -1;
} else {
return Math.max(leftHeight, rightHeight) + 1;
}
}
}
版权声明
本文为[JaneRoad]所创,转载请带上原文链接,感谢
边栏推荐
- Visit 2020 PG Technology Conference
- 极验无感验证破解
- For programmers, those unfamiliar and familiar computer hardware
- What can CRM system help enterprises do?
- Using tail as time series to identify whales
- 害怕重构?都怪我太晚和你介绍该如何重构,现在我来了
- 嘉宾专访|2020 PostgreSQL亚洲大会阿里云数据库专场:王健
- What is the SRM system? SRM supplier management system functions
- Simple use of JMeter
- Coding style: SSM environment in MVC mode, code hierarchical management
猜你喜欢
proxy 的不完整polyfill
Function calculation advanced IP query tool development
Explanation of Z-index attribute
IP address SSL certificate
Error running app:Default Activity not found 解决方法
jt-day10
完美日记母公司逸仙电商招股书:重营销、轻研发,前三季度亏11亿
Optimization of commodity backstage system
Fire knowledge online answer activity small program
快来学习!个性化推荐系统开发指南(附网盘链接)
随机推荐
Detach ()
CUDA_ Get the specified device
Simple use of JMeter
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
Prometheus安装配置
CUDA_寄存器和局部存储器
mongodb内核源码实现、性能调优、最佳运维实践系列-command命令处理模块源码实现一
How SSL certificate and public IP address affect SEO
SQL filter query duplicate columns
The solution of polar experience insensitive verification
Important components of Apache Hadoop
If you need a million objects
leetcode之最后一个单词的长度
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
自己上手写性能测试工具(二)
推动中国制造升级,汽车装配车间生产流水线3D可视化
Promote China manufacturing upgrade, 3D visualization of production line in automobile assembly workshop
CUDA_常量内存
Bifrost 位点管理 之 异构中间件实现难点(1)
利用尾巴作为时间序列进行处理来识别鲸鱼