当前位置:网站首页>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;
}
}
边栏推荐
- 全网最全的低代码/无代码平台盘点:简道云、伙伴云、明道云、轻流、速融云、集简云、Treelab、钉钉·宜搭、腾讯云·微搭、智能云·爱速搭、百数云
- 【obs】libobs-winrt :CreateDispatcherQueueController
- selenium 元素信息
- The difference between ID selector and class selector
- Postman核心功能解析-参数化和测试报告
- 大厂面试必备技能,2022Android不死我不倒
- What do software test engineers do? How about the prospect of treatment?
- Float.floatToRawIntBits的返回值具体意思,将float转为byte数组
- How to apply smart contracts more wisely in 2022?
- Common operators and operator priority
猜你喜欢
太牛了,看这篇足矣了
面试官:Redis中集合数据类型的内部实现方式是什么?
【C语言】字符串函数及模拟实现strlen&&strcpy&&strcat&&strcmp
The city chain technology Digital Innovation Strategy Summit was successfully held
Recommended collection, my Tencent Android interview experience sharing
Jvmrandom cannot set seeds | problem tracing | source code tracing
How to apply smart contracts more wisely in 2022?
测试外包公司怎么样?
third-party dynamic library (libcudnn.so) that Paddle depends on is not configured correctl
Debezium series: record the messages parsed by debezium and the solutions after the MariaDB database deletes multiple temporary tables
随机推荐
太牛了,看这篇足矣了
PG basics -- Logical Structure Management (user and permission management)
Tasks in GStreamer
不愧是大佬,字节大牛耗时八个月又一力作
Is it safe for Guosen Securities to open an account online?
软件测试是干什么的?学习有啥要求?
Inventory of the most complete low code / no code platforms in the whole network: Jiandao cloud, partner cloud, Mingdao cloud, Qingliu, xurong cloud, Jijian cloud, treelab, nailing · Yida, Tencent clo
Thread pool parameters and reasonable settings
aggregate
力扣 1200. 最小绝对差
Fundamentals of shell programming (Part 8: branch statements -case in)
[AI framework basic technology] automatic derivation mechanism (autograd)
安信证券在网上开户安全吗?
【硬核干货】数据分析哪家强?选Pandas还是选SQL
Django uses mysqlclient service to connect and write to the database
浅浅的谈一下ThreadLocalInsecureRandom
测试的核心价值到底是什么?
selenium 元素信息
Jvmrandom cannot set seeds | problem tracing | source code tracing
Using repositoryprovider to simplify the value passing of parent-child components