当前位置:网站首页>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;
}
}
边栏推荐
- Winter vacation daily question -- a single element in an ordered array
- Méthode de calcul de la connexion MSSQL de la carte esp32c3
- Sword finger offer 14- I. cut rope
- Example 072 calculation of salary it is known that the base salary of an employee of a company is 500 yuan. The amount of software sold by the employee and the Commission method are as follows: Sales
- A. Div. 7
- LV1 previous life archives
- Iclr2022 | ontoprotein: protein pre training integrated with gene ontology knowledge
- Jerry's watch information type table [chapter]
- On Valentine's day, I code a programmer's exclusive Bing Dwen Dwen (including the source code for free)
- Hamburg University of Technology (tuhh) | intelligent problem solving as integrated hierarchical reinforcement learning
猜你喜欢

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

Gee import SHP data - crop image
![After listening to the system clear message notification, Jerry informed the device side to delete the message [article]](/img/0c/52816b75eb702c7c63966578ab4969.jpg)
After listening to the system clear message notification, Jerry informed the device side to delete the message [article]

Basic editing specifications and variables of shell script

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

JVM performance tuning and practical basic theory - medium

中電資訊-信貸業務數字化轉型如何從星空到指尖?

Network communication basic kit -- IPv4 socket structure

C # learning notes: structure of CS documents

Crawler practice website image batch download
随机推荐
The contact data on Jerry's management device supports reading and updating operations [articles]
Final consistency of MESI cache in CPU -- why does CPU need cache
[untitled] the relationship between the metauniverse and digital collections
Node solves cross domain problems
Properties of binary trees (numerical aspects)
A. ABC
Sequence sorting of basic exercises of test questions
How to view the computing power of GPU?
Buuctf QR code
The difference between lambda expressions and anonymous inner classes
15. System limitations and options
[software implementation series] software implementation interview questions with SQL joint query diagram
How to subcontract uniapp and applet, detailed steps (illustration) # yyds dry goods inventory #
Create template profile
ZABBIX API pulls the values of all hosts of a monitoring item and saves them in Excel
mysql使用視圖報錯,EXPLAIN/SHOW can not be issued; lacking privileges for underlying table
Pytoch residual network RESNET
Description of setting items of Jerry [chapter]
Flex flexible layout, box in the middle of the page
13. Time conversion function