[LeetCode]110. Balanced Binary Tree

CentOS7 内核升级 可在http://ftp.sjtu.edu.cn/sites/elrepo.org/linux/kernel/el7/x86_64/RPMS选择对应架构平台不受影响的内核版本文件下载,然后离线安装,也可通过yum下载包后进行安装。通过 #uname -sr ,可以看到当前的内核版本 Linux 3.10.0-1160.el7.x86_64。首先找台能访问互联网的机器,通过联网的机器进行内核包下载。lt:longterm的缩写:长期维护版;ml:mainline的缩写:最新稳定版; 阅读详情

[LeetCode]110. Balanced Binary Tree

题目描述

这里写图片描述、

思路

深搜
初始化为true
当比较左右子树节点差的绝对值大于1的时候赋值为false

代码

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;

struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

class Solution {
public:
    TreeNode* vectorToTree(vector<int> nums){
        if (nums.empty())
            return NULL;
        TreeNode* root = new TreeNode(nums[0]);
        queue<TreeNode*> q;
        q.push(root);

        for (int i = 1; i < nums.size();) {
            int curSize = q.size();
            while (curSize--) {
                TreeNode* node = q.front();
                q.pop();

                if (nums[i]) {
                    node->left = new TreeNode(nums[i]);
                    q.push(node->left);
                }
                else
                    node->left = NULL;
                ++i;
                if (i == nums.size())
                    break;
                if (nums[i]) {
                    node->right = new TreeNode(nums[i]);
                    q.push(node->right);
                }
                else
                    node->right = NULL;
                ++i;
                if (i == nums.size())
                    break;
            }
        }
        return root;
    }

    bool isBalanced(TreeNode* root) {
        maxDepth(root);
        return flag;
    }
    int maxDepth(TreeNode* root) {
        if (root == NULL)
            return 0;
        int leftDepth = maxDepth(root->left),
            rightDepth = maxDepth(root->right);
        if (abs(leftDepth - rightDepth) > 1)
            flag = false;
        return max(leftDepth + 1, rightDepth + 1);
    }
private:
    bool flag = true;
};

int main() {
    vector<int> nums = { 1, 0, 1, 1 };
    Solution s;
    TreeNode* root = s.vectorToTree(nums);
    cout << s.isBalanced(root) << endl;

    system("pause");
    return 0;
}
深度学习-176-知识图谱技术之langchain与neo4j的嵌入向量Neo4jVector 使用Neo4jVector将输入的文档向量化,并创建向量索引,然后进行语义检索。 阅读详情

相关推荐

Windows10开机无限蓝屏(SrtTrail.txt)

这个问题已经有很多的大佬发过解决办法了,这里仅仅作为自己日后方便查找所写文章 无法开机——自动修复失败,报SrtTrail.txt文件错误,且陷入无限蓝屏的循环 事故 故事的起因是这样的: 开开心心的组装电脑,花了几天时间来配置,然后发现USB3.0的接口太少,于是在某宝上搞到一张pcie转7口USB3.0的转接卡,往电脑上一插 插电,开机,电流轻松跑上去 然后,它和原来的磁盘阵列卡冲突,跳过阵列卡自检直接进入系统,boom,就导致了这场悲剧 解决方法 在蓝屏的界面找到:疑难解答->通过命令提示符 使

Holo__的博客 3万+

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

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

motongxue的博客 246

基于Python的网络拓扑图绘制

最近写论文画了许多图,在这里记录一些。当然,如果仅仅是展示性图片的话也可以使用visio,但是这里我仍然想探究一下如何使用pyhon画出美观的网络拓扑图。(1)根据邻接矩阵画出网络拓扑图(2)画出网络拓扑图,并对节点和链路进行表示,包括对节点和链路的标号,颜色区分,以及尺寸区分。

m0_53961910的博客 7096

LeetCode 110. Balanced Binary Tree

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

温迪今天刷题了吗 193

为什么你的SiC MOSFET仿真不准?可能是离子注入的‘沟道效应’在捣鬼(Silvaco BCA模块详解)

本文深入分析了SiC MOSFET仿真中离子注入沟道效应的关键影响,揭示了Silvaco BCA模块在模拟SiC晶向和角度参数时的核心设置要点。通过详细解析晶圆偏切与注入角度的等效关系,提供优化MOSFET沟道区注入仿真的五步实战方法,帮助工程师将阈值电压仿真误差从26%降低到5%以内。

weixin_26789847的博客 410

基于虚拟阻抗重塑的构网型VSG变流器SISO序阻抗建模与宽频振荡抑制策略分析(面向高比例新能源并网场景)

本文深入探讨了基于虚拟阻抗控制的构网型VSG变流器SISO序阻抗建模技术及其在高比例新能源并网场景中的宽频振荡抑制策略。通过虚拟阻抗重塑,有效改善了变流器的阻抗特性,解决了次/超同步频段的振荡问题,并提供了参数设计、阻抗扫频验证等实用技巧,为新能源并网系统的稳定性提供了可靠解决方案。

weixin_29268385的博客 252

Java实现LeetCode 110. Balanced Binary Tree

/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { publ...

南 墙 1万+

【乐企-业务篇】乐企开票具体代码实现

【代码】【乐企-业务篇】乐企开票具体代码实现。

九七的博客 493

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

【STK】手把手教你利用STK进行进行光电红外仿真02-STK/EOIR模块属性之一 传感器选项设置

EOIR传感器可以从空间几何属性、光谱属性、光学属性、辐射属性四个方面进行设置,并且可以设置信号传播路径中的大气特性,包括大气模型、气溶胶模型、气象学可见性和大气湿度。在定义EOIR传感器时,需要Sensor——Definition属性页中,将Sensor Type设置为EOIR。单击下面的按钮打开传感器的EOIR配置页,可以对传感器的属性进行配置,此外,还可以在场景中视情况添加相关的中心体。路径辐射损耗影响上述特性的计算,计算的结果考虑了传感器、目标位置的影响,但是还没考虑地球临边背景的影响。

weixin_44184852的博客 3201

从数据库到数据仓库:数据仓库导论

本文为数据仓库导论,旨在介绍数据仓库的基本理念和应用场景,帮助读者理解数据仓库的重要性及其在企业中的实际应用。

探索大数据世界 - 深入解析数据存储、分布式计算与人工智能 2786

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

Adams Car——Adams car与Simulink联合仿真

Adams与simulink悬架联合仿真

Mr.邹的博客 2127

LeetCode 110 Balanced Binary Tree

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

wangxiaobupt的专栏 621

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

LeetCode //C - 110. Balanced Binary Tree

【代码】LeetCode //C - 110. Balanced Binary Tree。

Made in Code 1023

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

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

LeetCode110—Balanced Binary Tree

LeetCode110—Balanced Binary Tree

上善若水 456

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

Leetcode110 Balanced Binary Tree

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

_Lina的博客 213

Leetcode 110. Balanced Binary Tree

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 left and right subtr...

fengran_mark的博客 281

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编写程序的艺术 2222

leetcode 110. Balanced Binary Tree

头文件在http://blog.csdn.net/charles1e/article/details/56479700/* leetcode 110. Balanced Binary Tree Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree

编码之路 434

LeetCode110——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

代码菌的blog 1649

闯关leetcode——110. Balanced Binary Tree

平衡二叉搜索树(英语:Balanced Binary Search Tree)是一种结构平衡的二叉搜索树,它是一种每个节点的左右两子树高度差都不超过1的二叉树。})内完成插入、查找和删除操作,最早被发明的平衡二叉搜索树为AVL树。树的高度可以通过下面算法获得,即左右子树中最高高度+1为本节点高度。这题的解法就是实现:检验所有节点的左右子树高度差。这题就是要检验一棵树是否是平衡二叉树。然后递归检测每个节点是否符合。

方亮的专栏 1150

【leetcode】110. Balanced Binary Tree【java】

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

陈善亮的博客 676

leetcode[110] Balanced Binary Tree

判断一棵树是不是平衡二叉树。 思路:递归。 每个节点的左右子树是平衡二叉树,并且左右子树的高度相差不超过一。 /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * Tree...

101

leetcode-110-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 subt

早睡早起好少年 405
上一篇: [LeetCode]109. Convert Sorted List to Binary Search Tree
下一篇: [LeetCode]415. Add Strings
charon____
博客等级 码龄11年 3粉丝 202原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值