当前位置:网站首页>[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]所创,转载请带上原文链接,感谢
边栏推荐
- Important components of Apache Hadoop
- ES6, ES7, es8 Learning Guide
- 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
- 必看!RDS 数据库入门一本通(附网盘链接)
- leetcode之最后一个单词的长度
- 痞子衡嵌入式:RT-UFL - 一个适用全平台i.MXRT的超级下载算法设计
- mongodb内核源码实现、性能调优、最佳运维实践系列-command命令处理模块源码实现一
- Mongodb kernel source code implementation, performance tuning, best operation and maintenance practice series command processing module source code implementation 1
- One image can hold 16x16 words! ——Transformers for large scale image scaling recognition (a brief review of ICLR 2021 papers)
- Functional guide for temporary users and novices of PL / SQL developer
猜你喜欢

Promote China manufacturing upgrade, 3D visualization of production line in automobile assembly workshop

接缝雕刻算法:一种看似不可能的图像大小调整方法

Three ways to solve coursera video unable to watch

《Python Cookbook 3rd》笔记(2.1):使用多个界定符分割字符串

获取List集合对象中某一列属性值

Exception: invalid or unexpected token

Fire knowledge online answer activity small program

Validation failed for one or more entities. See ‘EntityValidationErrors’解决方法

Python prompt attributeerror or depreciation warning: This module was degraded solution

会展云技术解读 | 面对突发事故,APP 如何做好崩溃分析与性能监控?
随机推荐
[Python learning manual notes] 001. Preface to Python
z-index属性详解
What is the architecture of a live broadcast system worth more than $1 billion?
初级工程师如何在职场生存
恒讯科技浅谈:出现服务器宕机的处理方式
表单验证,为避免全局污染,少定义全局变量写法
Come and learn! Development Guide for personalized recommendation system (with internet disk link)
Operation and design of rights management in ERP
JMeter的简单使用
编码风格:Mvc模式下SSM环境,代码分层管理
CUDA_共享内存、访存机制、访问优化
eleven point nine
What is the SRM system? SRM supplier management system functions
Yixian e-commerce prospectus of perfect diary parent company: focusing on marketing and ignoring R & D, with a loss of 1.1 billion in the first three quarters
Detach ()
C++异常实现机制
If you need a million objects
CUDA_ Shared memory, memory access mechanism, access optimization
What can CRM system help enterprises do?
So what should investors do with the current market? Now a new investment outlet is coming!