当前位置:网站首页>[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]所创,转载请带上原文链接,感谢
边栏推荐
- 自己上手写性能测试工具(二)
- Day85: Luffy: shopping cart switching price according to different validity period & shopping cart deletion operation & price settlement & foreplay of order page
- 痞子衡嵌入式:RT-UFL - 一个适用全平台i.MXRT的超级下载算法设计
- Notes on Python cookbook 3rd (2.2): String start or end match
- What is the SRM system? SRM supplier management system functions
- mongodb内核源码实现、性能调优、最佳运维实践系列-command命令处理模块源码实现一
- CUDA_存储器模型
- 将Map中对应的key和value赋值到对象中
- Coding style: SSM environment in MVC mode, code hierarchical management
- 表单验证,为避免全局污染,少定义全局变量写法
猜你喜欢

SQL case conversion, remove the space before and after

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

Gets the property value of a column in the list collection object

Prometheus installation configuration

The length of the last word in leetcode

Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom

Aikang Guobin denounced Guoxin Securities report as untrue and sent a lawyer's letter

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

asp.net Using serilog in core and customizing enrich

树莓派鼓捣记 - 设置 wifi
随机推荐
实验2
Brief analysis of LinkedList source code
proxy 的不完整polyfill
假如需要一百万个对象
[leetcode] 92 integer inversion
mongodb内核源码实现、性能调优、最佳运维实践系列-command命令处理模块源码实现一
Error running app: default activity not found solution
Come and learn! Development Guide for personalized recommendation system (with internet disk link)
CUDA_共享内存、访存机制、访问优化
So what should investors do with the current market? Now a new investment outlet is coming!
Important components of Apache Hadoop
One image can hold 16x16 words! ——Transformers for large scale image scaling recognition (a brief review of ICLR 2021 papers)
Unity使用transform.Rotate进行三维旋转角度出现偏差
自己上手写性能测试工具(二)
Must see! RDS database all in one
Assign the corresponding key and value in the map to the object
Three ways to solve coursera video unable to watch
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
YouTube subscription: solve the problem of incomplete height display of YouTube subscription button in pop-up window
完美日记母公司逸仙电商招股书:重营销、轻研发,前三季度亏11亿