当前位置:网站首页>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;
}
}
边栏推荐
- [leetcode daily question] a single element in an ordered array
- I don't know why it can't run in the project and how to change it
- Reading notes - learn to write: what is writing?
- Crawler practice website image batch download
- What is the student party's Bluetooth headset recommendation? Student party easy to use Bluetooth headset recommended
- Node write API
- [Yugong series] February 2022 attack and defense world advanced question misc-83 (QR easy)
- When the watch system of Jerry's is abnormal, it is used to restore the system [chapter]
- The difference between int (1) and int (10)
- Global and Chinese market of handheld melanoma scanners 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢

A fan summed up so many interview questions for you. There is always one you need!

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

Will the memory of ParticleSystem be affected by maxparticles
![Jerry's watch listens to the message notification of the target third-party software and pushes the message to the device [article]](/img/8b/ff062f34d36e1caa9909c8ab431daf.jpg)
Jerry's watch listens to the message notification of the target third-party software and pushes the message to the device [article]

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.

Take you to master the formatter of visual studio code

What is the student party's Bluetooth headset recommendation? Student party easy to use Bluetooth headset recommended

1189. Maximum number of "balloons"

Applet graduation project is based on wechat classroom laboratory reservation applet graduation project opening report function reference

SQL statement
随机推荐
Jerry's watch listens to the message notification of the target third-party software and pushes the message to the device [article]
String: LV1 eat hot pot
Data collection and summary
Writeup (real questions and analysis of ciscn over the years) of the preliminary competition of national college students' information security competition
Small program graduation project based on wechat reservation small program graduation project opening report reference
What is the student party's Bluetooth headset recommendation? Student party easy to use Bluetooth headset recommended
Key knowledge of embedded driver
Comment la transformation numérique du crédit d'information de la Chine passe - t - elle du ciel au bout des doigts?
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?
在尋求人類智能AI的過程中,Meta將賭注押向了自監督學習
mysql使用視圖報錯,EXPLAIN/SHOW can not be issued; lacking privileges for underlying table
Key knowledge of C language
AI 助力藝術設計抄襲檢索新突破!劉芳教授團隊論文被多媒體頂級會議ACM MM錄用
[typora installation package] old typera installation package, free version
The difference between int (1) and int (10)
Libcblas appears when installing opencv import CV2 so. 3:cannot open shared object file:NO such file or directory
Will the memory of ParticleSystem be affected by maxparticles
Keep an IT training diary 054- opening and closing
The boss said: whoever wants to use double to define the amount of goods, just pack up and go
Take you to master the formatter of visual studio code