当前位置:网站首页>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;
}
}
边栏推荐
- 打新债在哪里操作开户是更安全可靠的呢
- webuploader文件上传 拖拽上传 进度监听 类型控制 上传结果监听控件
- The problem of returning the longtext field in MySQL and its solution
- 常用运算符与运算符优先级
- [FAQ] summary of common causes and solutions of Huawei account service error 907135701
- c——顺序结构
- Fundamentals of shell programming (Chapter 9: loop)
- 四万字长文说operator new & operator delete
- 力扣 729. 我的日程安排表 I
- SecureRandom那些事|真伪随机数
猜你喜欢
通过POI追加数据到excel中小案例
再忙不能忘安全
ACM getting started Day1
Win10 x64环境下基于VS2017和cmake-gui配置使用zxing以及opencv,并实现data metrix码的简单检测
How about testing outsourcing companies?
Summer Challenge database Xueba notes, quick review of exams / interviews~
Force buckle 1200 Minimum absolute difference
【无标题】
What do software test engineers do? How about the prospect of treatment?
PHP uses ueditor to upload pictures and add watermarks
随机推荐
C application interface development foundation - form control (6) - menu bar, toolbar and status bar controls
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
建议收藏,我的腾讯Android面试经历分享
Wildcard selector
Analysis of openh264 decoded data flow
How MySQL queries and modifies JSON data
秋招字节面试官问你还有什么问题?其实你已经踩雷了
redis集群模拟消息队列
Is it safe for Guohai Securities to open an account online?
[untitled]
司空见惯 - 英雄扫雷鼠
The difference between ID selector and class selector
The city chain technology Digital Innovation Strategy Summit was successfully held
Force buckle 1200 Minimum absolute difference
SecureRandom那些事|真伪随机数
安卓面试宝典,2022Android面试笔试总结
Is it safe to open a mobile stock account? Is it reliable?
成功入职百度月薪35K,2022Android开发面试解答
打新债在哪里操作开户是更安全可靠的呢
Jvmrandom cannot set seeds | problem tracing | source code tracing