当前位置:网站首页>leetcode刷题:二叉树11(平衡二叉树)
leetcode刷题:二叉树11(平衡二叉树)
2022-07-05 19:52:00 【涛涛英语学不进去】
110.平衡二叉树
给定一个二叉树,判断它是否是高度平衡的二叉树。
本题中,一棵高度平衡二叉树定义为:一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1。
示例 1:
给定二叉树 [3,9,20,null,null,15,7]

返回 true 。
示例 2:
给定二叉树 [1,2,2,3,3,null,null,4,4]

返回 false 。
这题,,,,好难,对我来说。
答案好巧妙。
使用递归
相当于层层往上返回,当都是叶子结点时,树的高度为0 + 1 ,否则就比较树的左右子树高度,如果差值 > 1,则是非平衡二叉树,不然取左右子树最高树 + 根结点 高度 为当前树的高度。

package com.programmercarl.tree;
import com.programmercarl.util.GenerateTreeNode;
import java.util.ArrayDeque;
import java.util.Deque;
/** * @ClassName IsBalanced * @Descriotion TODO * @Author nitaotao * @Date 2022/7/4 11:21 * @Version 1.0 * https://leetcode.cn/problems/balanced-binary-tree/ * 110. 平衡二叉树 **/
public class IsBalanced {
/** * 递归三部曲 * 1. 明确递归函数的参数和返回值 * 参数:当前传入节点 * 返回值:以当前传入结点为根结点的树的高度 * 如果当前传入结点为根结点的二叉树不是平衡二叉树,返回 -1 * <p> * 2. 明确终止条件 * 遇到空节点终止,返回0,表示当前结点以此结点为根结点的树的高度为0 * <p> * 3. 明确单层递归的逻辑 * 如何判断左右子树不平衡? * 左右子树高度差 > 1 返回 -1 * 返回当前高度差 * * @param root * @return */
public boolean isBalanced(TreeNode root) {
return getHeight(root) != -1;
}
// 高度为-1则不是平衡二叉树
public int getHeight(TreeNode root) {
//空结点高度为0
if (root == null) {
return 0;
}
int leftHeight = getHeight(root.left);
if (leftHeight == -1) {
return - 1;
}
int rightHeight = getHeight(root.right);
if (rightHeight == -1) {
return -1;
}
//高度差 > 1 , 则不对称
if (Math.abs(leftHeight - rightHeight) > 1) {
return -1;
}
//如果左右子树为平衡二叉树,返回树的整体高度 + 1
// 整体高度为 当前最高子树的高度 + 根结点的高度
return Math.max(leftHeight, rightHeight) + 1;
}
}
边栏推荐
- MMO项目学习一:预热
- 40000 word Wenshuo operator new & operator delete
- 全网最全的低代码/无代码平台盘点:简道云、伙伴云、明道云、轻流、速融云、集简云、Treelab、钉钉·宜搭、腾讯云·微搭、智能云·爱速搭、百数云
- Postman core function analysis - parameterization and test report
- 面试官:Redis中集合数据类型的内部实现方式是什么?
- 【无标题】
- 使用 RepositoryProvider简化父子组件的传值
- IBM大面积辞退40岁+的员工,掌握这十个搜索技巧让你的工作效率至上提高十倍
- 【obs】QString的UTF-8中文转换到blog打印 UTF-8 char*
- Multi branch structure
猜你喜欢

Millimeter wave radar human body sensor, intelligent perception of static presence, human presence detection application

95后阿里P7晒出工资单:狠补了这个,真香...

MMO項目學習一:預熱

全网最全的低代码/无代码平台盘点:简道云、伙伴云、明道云、轻流、速融云、集简云、Treelab、钉钉·宜搭、腾讯云·微搭、智能云·爱速搭、百数云

Based on vs2017 and cmake GUI configuration, zxing and opencv are used in win10 x64 environment, and simple detection of data matrix code is realized

面试官:Redis中集合数据类型的内部实现方式是什么?

四万字长文说operator new & operator delete

【无标题】

Recommended collection, my Tencent Android interview experience sharing

【C语言】字符串函数及模拟实现strlen&&strcpy&&strcat&&strcmp
随机推荐
webuploader文件上传 拖拽上传 进度监听 类型控制 上传结果监听控件
司空见惯 - 英雄扫雷鼠
Flume series: interceptor filtering data
Django uses mysqlclient service to connect and write to the database
SecureRandom那些事|真伪随机数
Float.floatToRawIntBits的返回值具体意思,将float转为byte数组
No matter how busy you are, you can't forget safety
redis集群模拟消息队列
PHP uses ueditor to upload pictures and add watermarks
The binary string mode is displayed after the value with the field type of longtext in MySQL is exported
[hard core dry goods] which company is better in data analysis? Choose pandas or SQL
Bitcoinwin (BCW) was invited to attend Hanoi traders fair 2022
S7-200SMART利用V90 MODBUS通信控制库控制V90伺服的具体方法和步骤
打新债在哪里操作开户是更安全可靠的呢
图嵌入Graph embedding学习笔记
函数的概念及语法
IBM大面积辞退40岁+的员工,掌握这十个搜索技巧让你的工作效率至上提高十倍
[Collection - industry solutions] how to build a high-performance data acceleration and data editing platform
c——顺序结构
acm入门day1