当前位置:网站首页>Leetcode-110-balanced binary tree
Leetcode-110-balanced binary tree
2022-06-11 21:28:00 【z754916067】
subject

Ideas
- The idea is to traverse the binary tree first , Return to the maximum height of the subtree from the bottom to the top , If a subtree is not a balanced tree, then “ prune ” , Go straight up .
- If the height difference between left and right ≥2 : Then return to -1, representative This subtree is not a balanced tree , Then go back up It's all balanced trees Will return non -1 Value
Code
public boolean isBalanced(TreeNode root) {
return recur(root) != -1;
}
// The idea is to traverse the binary tree first , Return to the maximum height of the subtree from the bottom to the top , If a subtree is not a balanced tree, then “ prune ” , Go straight up .
private int recur(TreeNode root) {
// Null pointer returns 0
if (root == null) return 0;
int left = recur(root.left);
if(left == -1) return -1;
int right = recur(root.right);
if(right == -1) return -1;
// If the height difference between the left and right subtrees of the current node <2 This indicates that the current node is a balanced subtree
// Return to node root Is the maximum height of the subtree of the root node , That is node root The maximum height of the left and right subtrees plus 1
// But if the height difference between the left and right ≥2 : Then return to -1, representative This subtree is not a balanced tree , Then go back up It's all balanced trees Will return non -1 Value
return Math.abs(left - right) < 2 ? Math.max(left, right) + 1 : -1;
}
边栏推荐
- Using the sap ui5 cli command line tool to build and run SAP ui5 applications
- LabVIEW控制Arduino实现红外测距(进阶篇—6)
- Experiment 10 Bezier curve generation - experiment improvement - control point generation of B-spline curve
- 一个Golang的私有库设置问题
- JVM object allocation policy TLAB
- flutter系列之:flutter中常用的container layout详解
- 2021-09-11 训练场补题
- table_ Display status
- LabVIEW Arduino电子称重系统(项目篇—1)
- LeetCode-104-二叉树的最大深度
猜你喜欢

Goland中在文件模板中为go文件添加个人声明

关于斜率优化

LabVIEW Arduino电子称重系统(项目篇—1)

Goto statement of go language

Deriving Kalman filter from probability theory

JVM|运行时数据区;程序计数器(PC寄存器);

How to Load Data from CSV (Data Preparation Part)

Three waves of changes in cloud computing

Test plans and test cases

JVM|虚拟机栈(局部变量表;操作数栈;动态链接;方法的绑定机制;方法的调用;方法返回地址)
随机推荐
Redis basic data type (set)
[Part 15] use and basic principle of forkjoinpool [key]
如何创建最简单的 SAP Kyma Function
一个Golang的私有库设置问题
ASCII码对照表
How to Load Data from CSV (Data Preparation Part)
Answer fans' questions | count the number and frequency of letters in the text
[advanced C language] integer storage in memory
flutter系列之:flutter中常用的container layout详解
Add personal statement for go file in file template in Golan
Part I physical layer
JVM|前言介绍
SQL的语法
2022年6月9日 16:29:41 日记
2021牛客多校5 Double Strings
作为一名 ABAP 资深顾问,下一步可以选择哪一门 SAP 技术作为主攻方向?
JVM|本地方法接口;本地方法栈
使用 SAP UI5 CLI 命令行工具构建和运行 SAP UI5 应用
As a senior abap consultant, which SAP technology can be selected as the main direction in the next step?
Solve the problem of img 5px spacing