当前位置:网站首页>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;
}
}
边栏推荐
- Format character%* s
- Global and Chinese markets of advanced X-ray inspection system (Axi) in PCB 2022-2028: Research Report on technology, participants, trends, market size and share
- Network byte order
- Small program graduation design is based on wechat order takeout small program graduation design opening report function reference
- G3 boiler water treatment registration examination and G3 boiler water treatment theory examination in 2022
- Question C: Huffman tree
- Comment la transformation numérique du crédit d'information de la Chine passe - t - elle du ciel au bout des doigts?
- Libcblas appears when installing opencv import CV2 so. 3:cannot open shared object file:NO such file or directory
- What is the intelligent monitoring system of sewage lifting pump station and does it play a big role
- Small program graduation project based on wechat reservation small program graduation project opening report reference
猜你喜欢
Small program graduation project based on wechat reservation small program graduation project opening report reference
Buuctf QR code
C # learning notes: structure of CS documents
在尋求人類智能AI的過程中,Meta將賭注押向了自監督學習
Jerry's synchronous weather information to equipment [chapter]
Bacteriostatic circle scanning correction template
[Yugong series] February 2022 attack and defense world advanced question misc-84 (MySQL)
The boss said: whoever wants to use double to define the amount of goods, just pack up and go
When tidb meets Flink: tidb efficiently enters the lake "new play" | tilaker team interview
Node solves cross domain problems
随机推荐
Global and Chinese market of box seals 2022-2028: Research Report on technology, participants, trends, market size and share
A brief talk on professional modeler: the prospect and professional development of 3D game modeling industry in China
Override and virtual of classes in C #
LV1 Roche limit
Applet graduation design is based on wechat course appointment registration. Applet graduation design opening report function reference
Mobile phone battery - current market situation and future development trend
Jerry's update contact [article]
Sequence sorting of basic exercises of test questions
Yyds dry goods inventory override and virtual of classes in C
MySQL advanced SQL statement (1)
Should enterprises start building progressive web applications?
SQL statement
Solve the problem that the tabbar navigation at the bottom of vantui does not correspond to the page (window.loading.hash)
Idea if a class cannot be found, it will be red
From the 18th line to the first line, the new story of the network security industry
Applet graduation project is based on wechat classroom laboratory reservation applet graduation project opening report function reference
17. File i/o buffer
2022 R2 mobile pressure vessel filling certificate examination and R2 mobile pressure vessel filling simulation examination questions
Save Private Ryan - map building + voltage dp+deque+ shortest circuit
Intel's new GPU patent shows that its graphics card products will use MCM Packaging Technology