[LeetCode]Balanced Binary Tree

creo二次开发使用映射 在creo进行二次开发时,一些无法直接通过访问底层代码来对特征进行操作,比如“镜像装配”,这个时候就可以使用映射(Mapkey)来完成一键镜像。 注:这是镜像装配的代码 ProError status; ProMacro macro; wstring Macro; strMacro = L"mapkey(continued) ~ Command ProCmdMirrorAssem ; mapkey(continued) ~ Activate mirror_component_dialog Sy 阅读详情
struct TreeNode {
	int val;
	TreeNode *left;
	TreeNode *right;
	TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

class Solution {
	//check if it is a balanced tree
	//so we check the diff between the max height of each branch
	//all compare can be done along the way down to calculate max height 
public:
	int CheckHeight(TreeNode* root, bool& ans)
	{
		if(!root || !ans)//if is is not a balance tree already, we just return
			return 0;
		int leftHeight = CheckHeight(root->left, ans);
		int rightHeight = CheckHeight(root->right, ans);
		if(abs(leftHeight-rightHeight) > 1)
			ans = false;
		return max(leftHeight, rightHeight)+1;
	}
    bool isBalanced(TreeNode *root) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        //get the height of each subtree
		bool ans = true;
		CheckHeight(root, ans);
		return ans;
    }
};

second time

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int isBalancedUtil(TreeNode* root, bool& isValid)
    {
        if(root == NULL) return 0;
        if(!isValid) return -1;
        int leftHeight = isBalancedUtil(root->left, isValid);
        int rightHeight = isBalancedUtil(root->right, isValid);
        if(abs(leftHeight-rightHeight) > 1) isValid = false;
        return max(leftHeight, rightHeight)+1;
    }
    bool isBalanced(TreeNode *root) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        bool ans = true;
        isBalancedUtil(root, ans);
        return ans;
    }
};


龙虾 Skill 技能库|OpenClaw+Hermes 全集成 一键调用所有 AI 技能 龙虾 Skill 技能库、OpenClaw、Hermes Agent、AI 技能集成、本地 AI 自动化、办公工具、数字员工 阅读详情

相关推荐

Arduino--土壤湿度传感器使用(电阻式)

最近买了一个土壤湿度检测传感器,实物图是这样的: 我买这个模块是想要得到土壤湿度的模拟量。但我买的时候忘了买左边的蓝色模块,我开始以为这个模块是有运放功能的,但实际它是一个电压比较器和一个分压电路。 其模块的接线如图: 该模块的四线是带模拟量输出的,三线的只输出一个到达阈值的标志量。该模块的原理图如下: 该模块的测试原理为:把土壤当成一个可变电阻,随着土壤含水量的增加,土壤电阻减小。而该传感器其实就是两个电极,用来接土壤这个可变电阻。一般湿度的土壤电阻大概在几百K左右,其串连的分压电阻应该在这个数量级。

weixin_41866783的博客 1万+

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

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

motongxue的博客 246

雪莉果的Axure原型工厂vol.006 中继器应用(二)——多选穿梭框

如果你用axure有一段时间了,那么你很快就能琢磨出一个大体设计流程,比如加一个中间过度的元件,来储存需要穿梭的内容;但是如果你再想一想,就会发现,这样做,似乎只能解决单选,那么多选需要怎么操作呢? 这时候,中继器就会告诉你,我还能做的更多。

weixin_43829792的博客 976

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

ColorOS13系统账户锁与密码锁破解全指南:从恢复模式到激活绕过

本文针对ColorOS13系统用户,详细解析了忘记锁屏密码(数字/图案锁)和遭遇OPPO账户锁(激活锁)的合法解决方案。核心是通过官方恢复模式进行格式化操作清除锁屏密码,并强调账户锁需通过密码找回或官方售后等正规途径解除。文章旨在帮助设备合法所有者恢复使用权,并提供了安全操作指南与数据备份建议。

weixin_28304023的博客 965

【Agent】多智能体框架MetaGPT入门教程

创建一个 Action 类的子类 SimpleWriteCode,子类中定义好 prompt,并解析返回结果。在 MetaGPT 中,

酒酿小圆子呀~ 3137

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

第7章: MMU notifier在AMD GPU驱动中的应用

AMD GPU驱动利用MMU Interval Notifier机制实现共享虚拟内存(SVM)和异构内存管理(HMM)。通过分析AMDGPU内存架构(VRAM、GTT、Userptr)和Buffer Object设计,重点探讨了Userptr零拷贝机制如何依赖MMU notifier保持CPU/GPU内存一致性。HMM框架进一步实现统一地址空间和透明页面迁移,AMDGPU通过amdgpu_hmm.c实现两种invalidate回调(GFX和HSA模式),处理不同场景下的内存失效同步问题。

图形/计算GPU的驱动与虚拟化技术分享园地 2468

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

第八届传智杯微信小程序开发挑战赛练习题库

本文是微信小程序开发练习题库,包含单选题、多选题和判断题三种题型。单选题主要考察小程序开发基础知识,如数组方法、组件使用、API调用等;多选题涵盖样式属性、页面跳转、数据处理等综合知识;判断题则测试开发者对小程序特性、生命周期、功能限制等概念的理解。题目内容涉及小程序开发的各个方面,包括组件使用、API调用、数据绑定、页面配置等核心知识点,适合开发者进行自我测试和知识巩固。

qq_73044452的博客 505

STM32CubeMX之emWin移植

“前面两章介绍了SDRAM和LTDC的使用,本篇文章将介绍emWin移植到STM32。”硬件环境:STM32F429IGT6软件环境:STM32CubeMX v5.5.0...

zhang062061的专栏 2826

LeetCode Balanced Binary Tree

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

DRFish 1143

华为OD机试真题 新系统【查找能被整除的最大整数】

华为OD新系统机试真题【查找能被整除的最大整数】,提供C语言、Java、C++、Python、Go、JavaScript源码实现,并提供完整思路讲解,以及在线OJ模拟做题。华为OD机试真题【查找能被整除的最大整数】

qq_45776114的博客 672

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

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

[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

华为的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

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

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 never diffe

每天都要有进步 192

[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

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

wangxiaobupt的专栏 621

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

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的专栏 1521

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

Leetcode110 Balanced Binary Tree

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

_Lina的博客 213

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]Anagrams
下一篇: [LeetCode]Best Time to Buy and Sell Stock
AI记忆
AI记忆 优质创作者: 人工智能技术领域 优质创作者: 人工智能技术领域 领域专家: 算法与数据结构技术领域 领域专家: 算法与数据结构技术领域
博客等级 码龄19年 6685粉丝 429原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI记忆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值