当前位置:网站首页>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;
}
}
边栏推荐
- 2022 R2 mobile pressure vessel filling certificate examination and R2 mobile pressure vessel filling simulation examination questions
- Question C: Huffman tree
- Introduction to graphics: graphic painting (I)
- Create template profile
- C learning notes: C foundation - Language & characteristics interpretation
- Iclr2022 | ontoprotein: protein pre training integrated with gene ontology knowledge
- The latest analysis of hoisting machinery command in 2022 and free examination questions of hoisting machinery command
- Global and Chinese market of small batteries 2022-2028: Research Report on technology, participants, trends, market size and share
- Prose article appreciation - the rain in the warm country has never changed into cold, hard and brilliant flowers. Knowledgeable people think he is monotonous, and he thinks he is unlucky, doesn't he?
- When the watch system of Jerry's is abnormal, it is used to restore the system [chapter]
猜你喜欢
SQL statement
Small program graduation project based on wechat reservation small program graduation project opening report reference
Applet graduation design is based on wechat course appointment registration. Applet graduation design opening report function reference
Basic editing specifications and variables of shell script
Crawler practice website image batch download
Write the first CUDA program
[software implementation series] software implementation interview questions with SQL joint query diagram
Life cycle of instance variables, static variables and local variables
Applet graduation project is based on wechat classroom laboratory reservation applet graduation project opening report function reference
[leetcode daily question] a single element in an ordered array
随机推荐
Setting function of Jerry's watch management device [chapter]
Will the memory of ParticleSystem be affected by maxparticles
Jerry's synchronous weather information to equipment [chapter]
It's corrected. There's one missing < /script >, why doesn't the following template come out?
Dans la recherche de l'intelligence humaine ai, Meta a misé sur l'apprentissage auto - supervisé
Example 073 square sum value judgment programming requires the input of a and B, if a ²+ b ² If the result of is greater than 100, a is output ²+ b ² Value, otherwise output the result of a + B.
Override and virtual of classes in C #
Pytoch residual network RESNET
Mysql-15 aggregate function
Keep an IT training diary 054- opening and closing
[Yugong series] February 2022 attack and defense world advanced question misc-83 (QR easy)
Iclr2022 | ontoprotein: protein pre training integrated with gene ontology knowledge
Career development direction
Sword finger offer 14- I. cut rope
Key knowledge of C language
Bacteriostatic circle scanning correction template
中電資訊-信貸業務數字化轉型如何從星空到指尖?
Why is the operation unsuccessful (unresolved) uncaught syntaxerror: invalid or unexpected token (resolved)
MySQL advanced SQL statement (1)
Database concept and installation