LeetCode 之 Balanced Binary Tree

告别Failed to start错误!保姆级教程:用Ubuntu20.04安装盘搞定GRUB与系统服务修复 本文提供Ubuntu20.04系统启动失败的专业修复指南,详细讲解如何利用安装盘解决GRUB与系统服务问题,避免重装系统导致的数据丢失。涵盖分区挂载、chroot环境操作、服务诊断与修复等实用技巧,帮助用户高效恢复系统正常运行。 阅读详情

题目链接Balanced Binary Tree
判断一个二叉树是否为平衡二叉树
平衡二叉树的定义为:每个节点的两个子树的深度相差不会超过1,即根节点的两个子树的深度相差不超过1,且两个子树也都是平衡二叉树。
那么使用递归的思想,先判断两个子树的深度差,再使用递归算法判断两个子树是否为平衡的
代码如下:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean isBalanced(TreeNode root) {
        if(root == null)
            return true;
        if(depth(root.left)-depth(root.right)>1||depth(root.right)-depth(root.left)>1)
            return false;
        else 
            return isBalanced(root.left)&&isBalanced(root.right);  
    }
    //计算树的深度
    public int depth(TreeNode root){
        if(root==null)
            return 0;
        int depthl = depth(root.left);
        int depthr = depth(root.right);
        int dep = 1 + ((depthl>depthr)?depthl:depthr);
        return dep;
    }
}
EXCEL VBA从入门到精通 第一章:VBA入门 在VBA中,需要使用关键字Dim来定义一个变量,后面跟上变量名和变量的数据类型。例如,下面的代码定义了一个整型变量x和一个字符串变量name在VBA中,使用关键字Const来定义常量,后面跟上常量名和常量的值。例如,下面的代码定义了一个整型常量MAX_NUM和一个字符串常量MESSAGE需要注意的是,常量一旦被定义,其值就不能被改变。 阅读详情

相关推荐

GOM Correlate拉伸试验后处理操作

本文描述了使用GOM Software 2022对拉伸试样进行后处理的操作,包括设置检测点、引伸计、输出应变数据到表格、输出报告为视频等

qq_45008924的博客 4662

LeetCode----Balanced Binary Tree

Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtre

whiterbear的专栏 704

【NLP】语言模型的发展历程 (1)

N-gram模型是一种基于统计语言模型的文本分析算法,它用于预测文本中下一个词出现的概率,基于前面出现的n-1个词的序列。这里的n代表序列中元素的数量,因此称为N-gram。Unigram:N=1,每个单词的出现概率独立计算,不考虑上下文。Bigram:N=2,基于前一个单词预测当前单词的联合概率模型。Trigram:N=3,考虑前两个单词来预测当前单词的联合概率模型,更复杂但可能更准确。

酒酿小圆子呀~ 1668

LeetCode 力扣 110. 平衡二叉树 balanced binary tree

大家觉得写还可以,可以点赞、收藏、关注一下吧! 也可以到我的个人博客参观一下,估计近几年都会一直更新!和我做个朋友吧!https://motongxue.cn 文章目录110. 平衡二叉树题目描述示例示例 1:示例 2:分析代码实现提交结果 110. 平衡二叉树 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树。 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1。 示例 示例 1: 给定二叉树 [3,9,20,null,null,15,7] 3

motongxue的博客 246

程序设计二(面向对象)_实训10_复杂类的实现_ArrayList类的实现

第1关:构造函数与析构函数的实现

2401_83406306的博客 819

浅谈车载控制器产线EOL实现方式

1、控制器生产产线上电路检测设计 2、客户总装产线检测设计

u012406840的博客 2万+

LeetCode | Balanced Binary Tree

题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees ofevery node nev

Allan的专栏 5026

让 AI 变聪明的 20 条指令:如何让大模型回答得更精准、更专业、更实事求是

让 AI 变聪明,本质上不是写几句神奇咒语,而是把你的任务表达得更清楚,把事实标准设得更严格,把输出格式规定得更明确,把不确定性管理得更认真,把历史对话利用得更充分。大模型的强项是语言生成、信息组织、模式归纳和方案扩展;弱项是事实核验、实时更新、责任判断和对模糊意图的准确猜测。好的指令,就是在用人的判断弥补模型的弱点,让模型把强项发挥在正确的地方。

Sumarua的博客 1480

LeetCode力扣 110. 平衡二叉树 Balanced Binary Tree 题解代码 JavaScript

问题https://leetcode-cn.com/problems/balanced-binary-tree/ 练习使用JavaScript解答 /** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } */ /** * @param {TreeNode} root * @retu.

Coding home - 漂流瓶jz 491

DSPF28335:从零到一,实战GPIO与外部中断的嵌入式系统交互设计

本文以德州仪器DSPF28335芯片为例,详细讲解了嵌入式系统中GPIO与外部中断(EXTI)的实战配置方法。通过一个完整的按键控制LED并触发ADC采样的项目,深入剖析了GPIO寄存器配置、三级中断机制(外设-PIE-CPU)以及中断服务函数编写的核心要点,帮助开发者掌握嵌入式交互设计的基石,并提供了关键的避坑指南与调试清单。

weixin_29169899的博客 115

Keras深度学习实战(32)——基于LSTM预测股价

股价预测是时间序列预测中最具挑战性的问题之一,研究股价预测问题具有重要的理论意义和应用价值。针对循环神经网络在股价预测中存在的长期依赖等缺点,本文引入了长短时记忆神经网络以提高股价预测的效果。同时,我们把有关公司的新闻标题的文本数据作为模型的输入变量时,发现预测效果更好,因此我们可以把有关公司的新闻标题的文本数据作为一个有效的输入变量。最后,通过利用 Keras 实现了以上两个股价预测模型,并进行了性能分析和对比。

盼小辉丶的博客 1万+

LeetCode——Balanced Binary Tree

LeetCode——Balanced Binary Tree #110 Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth o

BigSea 347

Yolov8-pose关键点检测:通用倒瓶颈(UIB)搜索块结合C2f二次创新 | 轻量化之王MobileNetV4

通用倒瓶颈(UIB)搜索块结合C2f二次创新

①答疑群聊服务;②YOLO大模型知识问答系统;③计算机视觉论文生成智能体; 613

LeetCode Balanced Binary Tree

判断一棵二叉树是否是平衡二叉树,只有当每个节点的左右两棵子树的高度差不大于1时,这棵树才是平衡的。

DRFish 1143

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

【LeetCode】Balanced Tree & Binary Search Tree

整合两道差不多的题目放上来,其中第一题是第二题的基础。 1. Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two

monkeyduck的专栏 2911

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

[LeetCode] Balanced Binary Tree

[Problem] Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node neve

1.02^365的成长裂变 3618

LeetCode 110. Balanced Binary Tree

LeetCode 110. Balanced Binary Tree(Easy) 主要知识点:平衡二叉树、DFS;优先级:5

温迪今天刷题了吗 193

[leetcode]Balanced Binary Tree

问题描述: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node nev

追梦船的专栏 848

leetcode-110 Balanced Binary Tree

Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as: a binary tree in which the depth of the two subtrees ofeverynode never diff...

大鹏的博客 284

LeetCode 110 Balanced Binary Tree

题目 题解 非递归 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(i

wangxiaobupt的专栏 621

Leetcode Balanced Binary Tree

Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never diffe

每天都要有进步 192

Balanced Binary Tree Leetcode Python

Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never diffe

hyperbolechi的专栏 1520

Leetcode110 Balanced Binary Tree

给定二叉树,判断它是否为平衡的,平衡的定义为,二叉树任意节点的两个子树的高度差都不超过1 思路 对于每个节点都要检查他的两个子树是否符合条件,这个流程符合dfs的思想。 方法1 递归对每个节点的左右子树求高度差 对于每个节点,左右子树高度差应该小于1,其子节点也要满足这个条件。 递归的终止条件是节点为空。 这种做法有重复的遍历。 class Solution { public...

_Lina的博客 212

Leetcode: Balanced Binary Tree

题目: Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never diffe

给永远比拿愉快 1080

LeetCode: Balanced Binary Tree 解题报告

Balanced Binary Tree Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of eve...

weixin_34032792的博客 225

leetcode 110 Balanced Binary Tree

Balanced Binary Tree Total Accepted: 63288 Total Submissions: 198315 My Submissions                         Given a binary tree, determine if it is height-balanced. For this problem, a height-bal...

shiter编写程序的艺术 2221

【leetcode】Balanced Binary Tree

题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node ne

日积月累 厚积薄发 724
上一篇: LeetCode 之 Implement strStr()
下一篇: LeetCode 之 String to Integer (atoi)
小菜鸟要高飞
博客等级 码龄12年 14粉丝 77原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值