当前位置:网站首页>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.
边栏推荐
- After the uncommitted transactions in the redo log buffer of MySQL InnoDB are persisted to the redo log, what happens if the transaction rollback occurs?
- Follow the promotional music MV of domestic tour in Thailand and travel to Bangkok like local people
- Technology sharing | wvp+zlmediakit realizes streaming playback of camera gb28181
- 618的省钱技术攻略 来啦 -体验场景 领取10元猫超卡!
- Esp32-c3 introductory tutorial problem ⑦ - fatal error: ESP_ Bt.h: no such file or directory ESP not found_ bt.h
- 4k-hdmi optical transceiver 1 channel [email protected] Hdmi2.0 optical transceiver HDMI HD video optical transceiver
- Go write file permission WriteFile (filename, data, 0644)?
- The redis keys command should be used with caution in the production environment. It is best to shield it
- 64 channel telephone +2-channel Gigabit Ethernet 64 channel PCM telephone optical transceiver voice telephone to optical fiber
- R语言使用构建有序多分类逻辑回归模型、ordinal.or.display函数获取有序逻辑回归模型的汇总统计信息(变量对应的优势比及其置信区间、以及假设检验的p值)、汇总统计结果保存到csv
猜你喜欢

MySQL使用ReplicationConnection导致的连接失效分析与解决

32-way telephone +2-way Gigabit Ethernet 32-way PCM telephone optical transceiver supports FXO port FXS voice telephone to optical fiber

Hanyuan hi tech 8-way telephone +1-way 100M Ethernet RJ11 telephone optical transceiver 8-way PCM telephone optical transceiver

AssetBundle resource management

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

90%的人都不懂的泛型,泛型的缺陷和应用场景

How to test the third-party payment interface?

Ablebits Ultimate Suite for Excel

You call this shit MQ?

What should testers do if the requirements need to be changed when the project is half tested?
随机推荐
Hanyuan hi tech 1-way uncompressed 4k-dvi optical transceiver 4K HD uncompressed DVI to optical fiber 4k-dvi HD video optical transceiver
2-optical-2-electric cascaded optical fiber transceiver Gigabit 2-optical-2-electric optical fiber transceiver Mini embedded industrial mine intrinsic safety optical fiber transceiver
kubernetes comfig subpath
You call this shit MQ?
windows 安装 MySQL
CDH mail alarm configuration
快速了解常用的非对称加密算法,再也不用担心面试官的刨根问底
Is it safe for flush to open an account online? What should we pay attention to
R language uses the polR function of mass package to build an ordered multi classification logistic regression model, and uses the summary function to obtain the summary statistical information of the
Technology sharing | wvp+zlmediakit realizes streaming playback of camera gb28181
R语言dplyr包filter函数过滤dataframe数据中指定数据列的内容包含指定字符串的数据行、基于grepl函数
Esp32-c3 introductory tutorial problems ⑧ - blufi_ example. c:244: undefined reference to `esp_ ble_ gap_ start_ advertising
腾讯的技术牛人们,是如何完成全面上云这件事儿的?
Cloud native essay deep understanding of ingress
Can cold plate, submerged and spray liquid cooling lead the development of high-performance computing?
Go寫文件的權限 WriteFile(filename, data, 0644)?
R语言使用MASS包的polr函数构建有序多分类logistic回归模型、使用exp函数和coef函数获取模型中每个变量(自变量改变一个单位)对应的优势比(odds ratio)
Esp32-c3 introductory tutorial problem ⑦ - fatal error: ESP_ Bt.h: no such file or directory ESP not found_ bt.h
Qunhui 10 Gigabit network configuration and test
A bug development means that the user will not operate like this, and there is no need to repair it. How should testers respond?