当前位置:网站首页>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;
}
}
边栏推荐
- Applet graduation project based on wechat selection voting applet graduation project opening report function reference
- Containerization technology stack
- Writeup (real questions and analysis of ciscn over the years) of the preliminary competition of national college students' information security competition
- Cancer biopsy instruments and kits - market status and future development trends
- Data collection and summary
- Sword finger offer 20 String representing numeric value
- AI 助力藝術設計抄襲檢索新突破!劉芳教授團隊論文被多媒體頂級會議ACM MM錄用
- Jerry's watch listens to the message notification of the target third-party software and pushes the message to the device [article]
- Small program graduation project based on wechat video broadcast small program graduation project opening report function reference
- Create template profile
猜你喜欢
Small program graduation project based on wechat examination small program graduation project opening report function reference
Applet graduation project is based on wechat classroom laboratory reservation applet graduation project opening report function reference
Bacteriostatic circle scanning correction template
Iclr2022 | ontoprotein: protein pre training integrated with gene ontology knowledge
How to subcontract uniapp and applet, detailed steps (illustration) # yyds dry goods inventory #
When the watch system of Jerry's is abnormal, it is used to restore the system [chapter]
Remember another interview trip to Ali, which ends on three sides
Small program graduation design is based on wechat order takeout small program graduation design opening report function reference
MySQL advanced (Advanced) SQL statement (I)
The automatic control system of pump station has powerful functions and diverse application scenarios
随机推荐
[typora installation package] old typera installation package, free version
Node write API
Advanced learning of MySQL -- Application -- index
Keep an IT training diary 054- opening and closing
Should enterprises start building progressive web applications?
The boss said: whoever wants to use double to define the amount of goods, just pack up and go
Buuctf QR code
Portable two-way radio equipment - current market situation and future development trend
求esp32C3板子連接mssql方法
Comment la transformation numérique du crédit d'information de la Chine passe - t - elle du ciel au bout des doigts?
Properties of binary trees (numerical aspects)
Magical usage of edge browser (highly recommended by program ape and student party)
Hamburg University of Technology (tuhh) | intelligent problem solving as integrated hierarchical reinforcement learning
Global and Chinese market of small batteries 2022-2028: Research Report on technology, participants, trends, market size and share
The automatic control system of pump station has powerful functions and diverse application scenarios
Node solves cross domain problems
Mobile phone battery - current market situation and future development trend
Yyds dry goods inventory override and virtual of classes in C
Flex flexible layout, box in the middle of the page
A brief talk on professional modeler: the prospect and professional development of 3D game modeling industry in China