当前位置:网站首页>Yyds dry inventory solution sword finger offer: judge whether it is a balanced binary tree
Yyds dry inventory solution sword finger offer: judge whether it is a balanced binary tree
2022-06-23 13:25:00 【51CTO】
1. sketch :
describe
Enter a node number of n Binary tree , Determine whether the binary tree is a balanced binary tree . ad locum , We just need to think about the balance , It is not necessary to consider whether it is a sort binary tree or a balanced binary tree (Balanced Binary Tree), It has the following properties : It is an empty tree or the absolute value of the height difference between its left and right subtrees does not exceed 1, And both the left and right subtrees are a balanced binary tree .
Sample explanation :
The sample binary tree is shown in the figure below , For a balanced binary tree
notes : We agree that an empty tree is a balanced binary tree .
Data range :, The nodes on the tree val It's worth it
requirement : Spatial complexity
, Time complexity
Input description :
Enter the root node of a binary tree
Return value description :
Output a Boolean value
Example 1
Input :
Return value :
Example 2
Input :
Return value :
2. Code implementation :
public class Solution {
public boolean IsBalanced_Solution(TreeNode root) {
if(root == null){
return true;
}
int left = deep(root.left);
int right = deep(root.right);
int v = Math.abs(left - right);
if(v > 1){
return false;
}
return IsBalanced_Solution(root.left) && IsBalanced_Solution(root.right);
}
private int deep(TreeNode root) {
if(root == null){
return 0;
}
return Math.max(deep(root.left), deep(root.right)) + 1;
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
边栏推荐
- Analyse et résolution des défaillances de connexion causées par MySQL utilisant replicationconnection
- 4-way telephone +1-way Gigabit Ethernet 4-way PCM telephone optical transceiver
- Can cold plate, submerged and spray liquid cooling lead the development of high-performance computing?
- 618的省钱技术攻略 来啦 -体验场景 领取10元猫超卡!
- 支持HomeKit、NFC:智汀智能门锁SL1仅需要149元
- 【深入理解TcaplusDB技术】单据受理之事务执行
- Technology sharing | wvp+zlmediakit realizes streaming playback of camera gb28181
- The redis keys command should be used with caution in the production environment. It is best to shield it
- POW共识机制
- What a talented company that can turn SAP system into a chicken rib!
猜你喜欢
![[website architecture] the unique skill of 10-year database design, practical design steps and specifications](/img/f2/061fa6dd42e57a121401e4f0cf1865.png)
[website architecture] the unique skill of 10-year database design, practical design steps and specifications

Wallys/DR6018-S/ 802.11AX MU-MIMO OFDMA / 2* GE PORTS/WIFI 6e / BAND DUAL CONCURRENT

"Four highs" of data midrange stability | startdt Tech Lab 18

What should testers do if the requirements need to be changed when the project is half tested?

华三交换机配置SSH远程登录

Analysis and solution of connection failure caused by MySQL using replicationconnection

windows 安装 MySQL

HomeKit支持matter协议,这背后将寓意着什么?

Playing in Singapore in the hot summer: an inventory of indoor attractions and good places for night trips

618的省钱技术攻略 来啦 -体验场景 领取10元猫超卡!
随机推荐
【网站架构】10年数据库设计浓缩的绝技,实打实的设计步骤与规范
互联网技术发展内卷后的出路——iVX的诞生
What are the criteria for judging the end of the test?
R语言dplyr包mutate_all函数将dataframe中的所有数值数值列(变量)乘以某一固定值并生成新的数据列,为新的数据列(变量)指定自定义后缀名称
R语言dplyr包filter函数过滤dataframe数据中指定数据列的内容包含指定字符串的数据行、基于grepl函数
You call this shit MQ?
Hanyuan hi tech 8-way telephone +1-way 100M Ethernet RJ11 telephone optical transceiver 8-way PCM telephone optical transceiver
Quickly understand the commonly used asymmetric encryption algorithm, and no longer have to worry about the interviewer's thorough inquiry
R语言使用构建有序多分类逻辑回归模型、ordinal.or.display函数获取有序逻辑回归模型的汇总统计信息(变量对应的优势比及其置信区间、以及假设检验的p值)、汇总统计结果保存到csv
Unity small demand - simple realization of imitation King Diamond (single draw)
理财产品长期是几年?新手最好买长期还是短期?
< Sicily> 1001. Rails
美国的国家公园概览
逆向调试入门-了解PE结构文件
HomeKit支持matter协议,这背后将寓意着什么?
同花顺网上开户安全吗,需要注意什么
Go写文件的权限 WriteFile(filename, data, 0644)?
R语言使用MASS包的polr函数构建有序多分类logistic回归模型、使用exp函数和coef函数获取模型中每个变量(自变量改变一个单位)对应的优势比(odds ratio)
#yyds干货盘点# 解决剑指offer: 判断是不是平衡二叉树
20000 words + 30 pictures | MySQL log: what is the use of undo log, redo log and binlog?