首页 > 其他分享 >闯关leetcode——110. Balanced Binary Tree

闯关leetcode——110. Balanced Binary Tree

时间:2024-10-20 15:18:43浏览次数:3  
标签:Binary right return tree Tree height 110 null root

大纲

  • 题目
    • 地址
    • 内容
  • 解题
    • 代码地址

题目

地址

https://leetcode.com/problems/balanced-binary-tree/description/

内容

Given a binary tree, determine if it is height-balanced.

A height-balanced binary tree is a binary tree in which the depth of the two subtrees of every node never differs by more than one.

Example 1:
在这里插入图片描述

Input: root = [3,9,20,null,null,15,7]
Output: true

Example 2:

Input: root = [1,2,2,3,3,null,null,4,4]
Output: false

Example 3:

Input: root = []
Output: true

Constraints:

  • The number of nodes in the tree is in the range [0, 5000].
  • -104 <= Node.val <= 104

解题

这题就是要检验一棵树是否是平衡二叉树。

平衡二叉搜索树(英语:Balanced Binary Search Tree)是一种结构平衡的二叉搜索树,它是一种每个节点的左右两子树高度差都不超过1的二叉树。它能在O(logn})内完成插入、查找和删除操作,最早被发明的平衡二叉搜索树为AVL树。

这题的解法就是实现:检验所有节点的左右子树高度差。
树的高度可以通过下面算法获得,即左右子树中最高高度+1为本节点高度。

    int height(TreeNode* root) {
        if (root == nullptr) return 0;
        return 1 + max(height(root->left), height(root->right));
    }

然后递归检测每个节点是否符合左右两子树高度差都不超过1的特点。

class Solution {
public:
    bool isBalanced(TreeNode* root) {
        if (root == nullptr) return true;
        return abs(height(root->left) - height(root->right)) <= 1 && isBalanced(root->left) && isBalanced(root->right);
    }
private:
    int height(TreeNode* root) {
        if (root == nullptr) return 0;
        return 1 + max(height(root->left), height(root->right));
    }
};

在这里插入图片描述

代码地址

https://github.com/f304646673/leetcode/tree/main/110-Balanced-Binary-Tree

标签:Binary,right,return,tree,Tree,height,110,null,root
From: https://blog.csdn.net/breaksoftware/article/details/142217094

相关文章

  • AI预测体彩排3采取888=3策略+和值012路或胆码测试10月20日升级新模型预测第110弹
            经过100多期的测试,当然有很多彩友也一直在观察我每天发的预测结果,得到了一个非常有价值的信息,那就是9码定位的命中率非常高,已到达90%的命中率,这给喜欢打私菜的朋友提供了极高价值的预测结果~当然了,大部分菜友还是走的正常渠道,因此,得想办法进行缩水,尽可能少的缩......
  • [LeetCode] 1545. Find Kth Bit in Nth Binary String
    Giventwopositiveintegersnandk,thebinarystringSnisformedasfollows:S1="0"Si=Si-1+"1"+reverse(invert(Si-1))fori>1Where+denotestheconcatenationoperation,reverse(x)returnsthereversedstringx,an......
  • [ARC185D] Random Walk on Tree 题解
    一个很套路的做法。思路题目要求走完整个树的时间,这并不好算,容易想到min-max容斥。依据min-max容斥,我们可以轻松把它转化成第一次走到所有子集的时间。考虑在这道题中,有什么特殊的。第一,任何包含根节点的子集答案都是零。第二,由于我们只关心第一次走到的点的时间,因此假......
  • 打卡信奥刷题(069)用C++工具信奥P11076[普及组/提高] 「FSLOI Round I」单挑
    「FSLOIRoundI」单挑题目背景Englishstatement.YoumustsubmityourcodeattheChineseversionofthestatement.小F和小S经常进行篮球单挑,但小S总是被盖帽。题目描述每次单挑的结果一定是小F获胜或者小S获胜,不存在平局的情况。由于小F和小S实......
  • luckfox1106初次使用
    luckfox1106初次使用下载rk驱动https://files.luckfox.com/wiki/Luckfox-Pico/Software/DriverAssitant_v5.12.zip安装驱动SD卡烧录工具https://files.luckfox.com/wiki/Luckfox-Pico/Software/SocToolKit_v1.98_20240705_01_win.zip右键以管理员方式运行......
  • 最小生成树(Minimum Spanning Tree,MST)初步
    定义连通图的最小生成树(MinimumSpanningTree,MST)为边权和最小的生成树。注意:只有连通图才有生成树,而对于非连通图,只存在生成森林。思路分为Kruskal与Prim两种算法。Kruskal从最小边权的边开始,按边权从小到大依次遍历。若当前边连接的两点不连通,加入此边。Prim每次选......
  • java_day14_HashSet、TreeSet、增强for循环、Map、HashMap、TreeMap、可变参数
    一、HashSetSet:HashSet:底层数据结构是哈希表,查找速度快,且元素唯一HashSet中的add方法实际上调用的是HashMap中的put方法底层和元素的hashCode方法值有关我们发现,底层判断待插入的元素是否已经存在哈希表中的方式是:将待插入的元素的哈希值与已经存......
  • AntDesign树形组件tree和输入input组件融合使用
    <a-tree :tree-data="selectItem.options.options" :replace-fields="{ children:'children', title:'label', code:'code' }" >......
  • 打开游戏缺少msvcp110.dll?游戏msvcp110.dll丢失的解决方法
    当我们沉浸在游戏世界中时,突然出现“msvcp110.dll丢失”这样的错误提示,无疑会让游戏体验大打折扣。这个问题通常是由于MicrosoftVisualC++RedistributablePackage未正确安装或损坏所致。msvcp110.dll是该包中的一个关键组件,许多游戏和应用都需要它来正常运行。下面一......
  • Map集合中的具体子类TreeMap
    一、TreeMap元素是一个键值对,可以去重并进行排序1.先编写一个Dog2类publicclassDog2{privateStringname;privateintage;publicDog2(){}publicDog2(Stringname,intage){this.name=name;......