当前位置:网站首页>[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]所创,转载请带上原文链接,感谢
边栏推荐
- JS label syntax jumps out of multiple loops
- Python prompt attributeerror or depreciation warning: This module was degraded solution
- JMeter的简单使用
- 编码风格:Mvc模式下SSM环境,代码分层管理
- Difficulties in heterogeneous middleware implementation of Bifrost site management (1)
- Important components of Apache Hadoop
- Gets the property value of a column in the list collection object
- So what should investors do with the current market? Now a new investment outlet is coming!
- 编码风格:Mvc模式下SSM环境,代码分层管理
- 实验2
猜你喜欢

一幅图像能顶16x16字!——用于大规模图像缩放识别的变压器(对ICLR 2021年论文的简要回顾)

一个名为不安全的类Unsafe

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

Coding style: SSM environment in MVC mode, code hierarchical management

Can public IP address and SSL certificate improve SEO?

Error running app: default activity not found solution

Day85: Luffy: shopping cart switching price according to different validity period & shopping cart deletion operation & price settlement & foreplay of order page

JT Jingtao project

推动中国制造升级,汽车装配车间生产流水线3D可视化

Simple use of JMeter
随机推荐
CUDA_ constant memory
Functional guide for temporary users and novices of PL / SQL developer
分布式文档存储数据库之MongoDB索引管理
Prometheus installation configuration
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
ES6, ES7, es8 Learning Guide
For programmers, those unfamiliar and familiar computer hardware
Error running app: default activity not found solution
Fire knowledge online answer activity small program
Notes on Python cookbook 3rd (2.2): String start or end match
leetcode之最后一个单词的长度
Bifrost 位点管理 之 异构中间件实现难点(1)
害怕重构?都怪我太晚和你介绍该如何重构,现在我来了
Common concepts and points for attention of CUDA
会展云技术解读 | 面对突发事故,APP 如何做好崩溃分析与性能监控?
假如需要一百万个对象
JT Jingtao project
实验2
C++ exception implementation mechanism
What can CRM system help enterprises do?