当前位置:网站首页>Sword finger offer 55 - ii balanced binary tree
Sword finger offer 55 - ii balanced binary tree
2022-07-18 11:09:00 【Ding Jiaxiong】
subject
Enter the root node of a binary tree , Judge whether the tree is a balanced binary tree . If the depth difference between the left and right subtrees of any node in a binary tree does not exceed 1, So it's a balanced binary tree .
Example 1:
Given binary tree [3,9,20,null,null,15,7]
3
/ \
9 20
/ \
15 7
return true .
Example 2:
Given binary tree [1,2,2,3,3,null,null,4,4]
1
/ \
2 2
/ \
3 3
/ \
4 4
return false .
Limit :
0 <= The number of nodes in a tree <= 10000
Answer key
class Solution:
def isBalanced(self, root: TreeNode) -> bool:
def recur(root):
if not root:
return 0
left = recur(root.left)
if left == -1:
return - 1
right = recur(root.right)
if right == -1:
return -1
return max(left , right) + 1 if abs(left - right) <= 1 else -1
return recur(root) != -1

边栏推荐
- 实验三 Servlet 相关技术
- P1088 [NOIP2004 普及组第四题] 火星人 ← next_permutation
- Pay attention to those potential system design problems
- There is only one day left to prepare for the examination of Guangxi Second Construction Engineering Co., Ltd. the first three pages of the examination of second-class cost engineer came and raised sc
- Chrome 插件开发
- Design of DC motor control system
- Algorithm In Interview
- Detailed steps for installing mysql8 in centos7.9
- 编程语言学习和使用的观点
- Pytorch分布式训练
猜你喜欢
随机推荐
Experiment 5 image segmentation and description
Ultrasonic ranging OLED display
Digital display of potentiometer based on ADC0832
基于ADC0832的电量指示电路
The digital tube displays numbers circularly
P1085 [NOIP2004 普及组第一题] 不高兴的津津 ← 模拟题
Keil mdk5, use of arm CC ---- >
【MySQL必知必会】条件语句
留心那些潜在的系统设计问题
接口测试——流程测试支持批量参数导入,测试效率直接拉满
The relationship between loss function and maximum likelihood estimation | understanding of cross entropy
Breathing lamp circuit based on 555 timer
IP static routing comprehensive experiment
元素的增删改查【DOM(二)】
第3章业务功能开发(导入市场活动,apache-poi)
笔记
The best time to buy and sell stocks
关于线程切换问题的一些思考总结
C: free(): 无效指针中止(核心转储)的思考
JVM简述 GC垃圾回收机制









