当前位置:网站首页>[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]所创,转载请带上原文链接,感谢
边栏推荐
- Raspberry pie drum set WiFi
- Make a home page
- 实验2
- 害怕重构?都怪我太晚和你介绍该如何重构,现在我来了
- 一幅图像能顶16x16字!——用于大规模图像缩放识别的变压器(对ICLR 2021年论文的简要回顾)
- CUDA_ Global memory and access optimization
- Can public IP address and SSL certificate improve SEO?
- Fire knowledge online answer activity small program
- 《Python Cookbook 3rd》笔记(2.1):使用多个界定符分割字符串
- 【CentOS7操作系统安全加固系列】第(2)篇
猜你喜欢
![Usage of [:] and [::] in Python](/img/3b/00bc81122d330c9d59909994e61027.jpg)
Usage of [:] and [::] in Python

C++ exception implementation mechanism

Incomplete Polyfill of proxy

Youtube订阅——解决在弹窗内使用Youtube订阅按钮高度显示不全的问题

day85:luffy:购物车根据有效期不同切换价格&购物车删除操作&价格结算&订单页面前戏

How much is the cost of CRM system?

Prometheus installation configuration

编码风格:Mvc模式下SSM环境,代码分层管理

The problem of looting by leetcode

2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
随机推荐
消防知识线上答题活动小程序复盘
表单验证,为避免全局污染,少定义全局变量写法
SRM系统是什么系统?SRM供应商管理系统功能
Bifrost 位点管理 之 异构中间件实现难点(1)
Mongodb kernel source code implementation, performance tuning, best operation and maintenance practice series command processing module source code implementation 1
C++ exception implementation mechanism
Prometheus installation configuration
Operation and design of rights management in ERP
恒讯科技浅谈:出现服务器宕机的处理方式
自己上手写性能测试工具(二)
Interviewer: what are cache penetration, cache avalanche and cache breakdown?
Python中[:]与[::]的用法
将Map中对应的key和value赋值到对象中
假如需要一百万个对象
leetcode之最后一个单词的长度
C++异常实现机制
函数计算进阶-IP查询工具开发
Notes on Python cookbook 3rd (2.2): String start or end match
Ineuos industrial interconnection platform, web configuration (ineuview) increases the function of importing and exporting engineering views, as well as optimization and repair. Release: v3.2.1
剑指offer之打印二叉搜索树中第k小的结点