当前位置:网站首页>Leetcode 110 balanced binary tree
Leetcode 110 balanced binary tree
2022-07-04 02:21:00 【baj001】
Ideas
- Balanced binary tree means that the height difference between the left and right subtrees does not exceed 1
- Building recursive functions recur
- When node by null, It will terminate directly
- Here are the detailed operations of recursive functions : Use left To save the height of the left subtree or whether it is -1, Then judge whether the left subtree is -1, If so, go back to -1, Similarly, right subtree . Finally back to : Judge the height difference between the left and right subtrees Less than 2, If less than 2, Returns the height of the current number , Instead, return to - 1
class Solution {
public boolean isBalanced(TreeNode root) {
// At this time, if the return value of the difference between the left and right depths of the root node is not -1, That's right
return recur(root) != -1;
}
public int recur(TreeNode node){
if(node == null) return 0;
int left = recur(node.left);
if(left == -1) return -1;
int right = recur(node.right);
if(right == -1) return -1;
// If the depth difference is less than 2 Represents a balanced binary tree , Then return to the current maximum depth , Otherwise return to -1
return Math.abs(left - right) < 2 ? Math.max(left, right) + 1 : -1;
}
}
边栏推荐
- Remote work guide
- From the 18th line to the first line, the new story of the network security industry
- 3D game modeling is in full swing. Are you still confused about the future?
- Will the memory of ParticleSystem be affected by maxparticles
- Ceramic metal crowns - current market situation and future development trend
- Libcblas appears when installing opencv import CV2 so. 3:cannot open shared object file:NO such file or directory
- On Valentine's day, I code a programmer's exclusive Bing Dwen Dwen (including the source code for free)
- Keep an IT training diary 055- moral bitch
- The requests module uses
- Advanced learning of MySQL -- Application -- storage engine
猜你喜欢

On Valentine's day, I code a programmer's exclusive Bing Dwen Dwen (including the source code for free)

Applet graduation design is based on wechat course appointment registration. Applet graduation design opening report function reference

MySQL advanced (Advanced) SQL statement (I)

Small program graduation project based on wechat reservation small program graduation project opening report reference

Applet graduation project based on wechat selection voting applet graduation project opening report function reference

What are the conditions for the opening of Tiktok live broadcast preview?

Comment la transformation numérique du crédit d'information de la Chine passe - t - elle du ciel au bout des doigts?

Take you to master the formatter of visual studio code

Valentine's Day - 9 jigsaw puzzles with deep love in wechat circle of friends

String & memory function (detailed explanation)
随机推荐
When the watch system of Jerry's is abnormal, it is used to restore the system [chapter]
Pytoch residual network RESNET
2022 R2 mobile pressure vessel filling certificate examination and R2 mobile pressure vessel filling simulation examination questions
Keep an IT training diary 055- moral bitch
Measurement fitting based on Halcon learning [4] measure_ arc. Hdev routine
Take you to master the formatter of visual studio code
Global and Chinese market of contour projectors 2022-2028: Research Report on technology, participants, trends, market size and share
String & memory function (detailed explanation)
What is the intelligent monitoring system of sewage lifting pump station and does it play a big role
Properties of binary trees (numerical aspects)
[typora installation package] old typera installation package, free version
Winter vacation daily question -- a single element in an ordered array
MySQL workbench use
Sword finger offer 20 String representing numeric value
It's corrected. There's one missing < /script >, why doesn't the following template come out?
Yyds dry goods inventory override and virtual of classes in C
Writeup (real questions and analysis of ciscn over the years) of the preliminary competition of national college students' information security competition
Portapack application development tutorial (XVII) nRF24L01 launch C
[software implementation series] software implementation interview questions with SQL joint query diagram
Format character%* s